Disable ok button on JOptionPane.dialog until user gives an input

Sanziana picture Sanziana · Jan 15, 2013 · Viewed 10.4k times · Source

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... ?

Answer

MadProgrammer picture MadProgrammer · Jan 15, 2013

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.

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

Updated

For example...

enter image description here

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);
            }
        });
    }
}