Sanziana
Sanziana

Reputation: 199

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

Upvotes: 13

Views: 12833

Answers (4)

MadProgrammer
MadProgrammer

Reputation: 347184

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

Upvotes: 28

Michael Dunn
Michael Dunn

Reputation: 816

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

wrong way to do it.

i.e. define 'what is a name' = can be anything.

so, what you're, in effect, trying to do is not accept an empty string,

and you do that as an error-check 'after' the OK button has been pressed.

if empty - pop-up error message/repeat input request/confirm cancel/whatever you want to do

Upvotes: -2

Archer
Archer

Reputation: 5147

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

Upvotes: 0

Nitesh Verma
Nitesh Verma

Reputation: 1815

Try searching for swinglabs or jGoodies libraries for Java. They have built in type for the thing you need.

Upvotes: 0

Related Questions