在JOptionPane.dialog上禁用ok按钮,直到用户输入 [英] Disable ok button on JOptionPane.dialog until user gives an input

查看:149
本文介绍了在JOptionPane.dialog上禁用ok按钮,直到用户输入的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要用户输入名称,我想要禁用确定按钮,直到给出一些输入。如何禁用它??

I need the user to input a name and I want to disable the ok button until some input is given. How can I disable it... ?

推荐答案

JOptionPane 允许你提供组件作为消息窗格和可以显示在其上的控件/选项。

JOptionPane allows you to supply a component as the message pane and the controls/options that can be displayed on it.

如果向消息组件添加正确的侦听器,那么您应该是能够影响用作选项的控件。

If you add the correct listeners to the message component, then you should be able to influence the controls that are used as options.

看看 JOptionPane.showOptionDialog(Component parentComponent,Object message,String title,int optionType,int messageType,Icon icon,Object [] options,Object initialValue)

已更新

例如......

public class TestOptionPane05 {

    public static void main(String[] args) {
        new TestOptionPane05();
    }

    protected JOptionPane getOptionPane(JComponent parent) {
        JOptionPane pane = null;
        if (!(parent instanceof JOptionPane)) {
            pane = getOptionPane((JComponent)parent.getParent());
        } else {
            pane = (JOptionPane) parent;
        }
        return pane;
    }

    public TestOptionPane05() {
        EventQueue.invokeLater(new Runnable() {
            @Override
            public void run() {
                try {
                    UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
                } catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
                }

                final JButton okay = new JButton("Ok");
                okay.addActionListener(new ActionListener() {
                    @Override
                    public void actionPerformed(ActionEvent e) {
                        JOptionPane pane = getOptionPane((JComponent)e.getSource());
                        pane.setValue(okay);
                    }
                });
                okay.setEnabled(false);
                final JButton cancel = new JButton("Cancel");
                cancel.addActionListener(new ActionListener() {
                    @Override
                    public void actionPerformed(ActionEvent e) {
                        JOptionPane pane = getOptionPane((JComponent)e.getSource());
                        pane.setValue(cancel);
                    }
                });

                final JTextField field = new JTextField();
                field.getDocument().addDocumentListener(new DocumentListener() {
                    protected void update() {
                        okay.setEnabled(field.getText().length() > 0);
                    }

                    @Override
                    public void insertUpdate(DocumentEvent e) {
                        update();
                    }

                    @Override
                    public void removeUpdate(DocumentEvent e) {
                        update();
                    }

                    @Override
                    public void changedUpdate(DocumentEvent e) {
                        update();
                    }
                });

                JOptionPane.showOptionDialog(
                                null, 
                                field, 
                                "Get", 
                                JOptionPane.YES_NO_OPTION, 
                                JOptionPane.QUESTION_MESSAGE, 
                                null, 
                                new Object[]{okay, cancel}, 
                                okay);
            }
        });
    }
}

这篇关于在JOptionPane.dialog上禁用ok按钮,直到用户输入的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆