4

I need to use a JOptionPane to give the user two options. Depending on previous actions though one of the buttons may need to be disabled.

Is it possible with JOptionPane to have the ability to set either of the buttons to be disabled or enabled?

How can I do this?

MadProgrammer
  • 343,457
  • 22
  • 230
  • 366
user2020457
  • 165
  • 1
  • 1
  • 13
  • 2
    Yes, and it's messy. Check [this](http://stackoverflow.com/questions/14334931/disable-ok-button-on-joptionpane-dialog-until-user-gives-an-input/14335083#14335083) for an example – MadProgrammer Jan 29 '13 at 05:28
  • 1
    While many things are possible with `JOptionPane` making tweaks and adjustments on it is usually impractical. In those cases it is better to use a `JDialog`. – Andrew Thompson Jan 29 '13 at 07:29
  • 1
    You can use a _JDialog_ customize it like a _JOptionPane_. Its simple and easy to implement. – Amarnath Jan 29 '13 at 07:49

1 Answers1

1

It's easy if you use JButtons:

    public class Test
{
    public static void main(String[] args)
    {
        final JButton option1 = new JButton("option1");
        final JButton option2 = new JButton("option2");
        option1.setEnabled(false);
        // option2.setEnabled(false);
        option1.addActionListener(new ActionListener()
        {
            @Override
            public void actionPerformed(ActionEvent arg0)
            {
                // code here
            }
        });
        option2.addActionListener(new ActionListener()
        {
            @Override
            public void actionPerformed(ActionEvent e)
            {
                // code here
            }
        });
        JOptionPane.showOptionDialog(null, "hello", "The Title", JOptionPane.NO_OPTION, JOptionPane.PLAIN_MESSAGE, null, new JButton[]
        { option1, option2 }, option1);
    }
}
wardva
  • 624
  • 9
  • 28