Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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

like image 372
Sanziana Avatar asked Jan 15 '13 09:01

Sanziana


2 Answers

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);
            }
        });
    }
}
like image 120
MadProgrammer Avatar answered Oct 11 '22 16:10

MadProgrammer


As far as I know this is impossible without overriding JOptionPane.

like image 43
Archer Avatar answered Oct 11 '22 15:10

Archer