I am trying to get the value returned by custom buttons passed to JOptionPane. However, the buttons I pass do not return a value at all. Only when the exit button is pressed does the value -1 return. I need this because I am changing the properties of buttons that are on or off. I guess I need buttons to somehow return some information to JOptionPane. Any idea?
JButton button1= new JButton("Button 1"); JButton button2= new JButton("Button 2"); button1.setEnabled(false); int value = JOptionPane.showOptionDialog(null, "Heres a test message", "Test", JOptionPane.YES_NO_OPTION, JOptionPane.QUESTION_MESSAGE, null, new Object[]{button1, button2}, button1); JOptionPane.showMessageDialog(null, "You entered " + value);
Nb This is related to my previous question - JOptionPane Gray Out One Button
I tried to set the value of the buttons, as you said, but they never return to OK or CANCEL.
Whenever you check the value of the buttons, they never return the value that I set for them.
JButton button1= new JButton("Button1"); JButton button2= new JButton("Button2"); button1.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { JOptionPane pane = getOptionPane((JComponent)e.getSource()); // set the value of the option pane pane.setValue(JOptionPane.OK_OPTION); } }); button2.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { JOptionPane pane = getOptionPane((JComponent)e.getSource()); // set the value of the option pane pane.setValue(JOptionPane.CANCEL_OPTION); } }); if (JOptionPane.showOptionDialog(null, "Pick a button", "Pick", JOptionPane.OK_CANCEL_OPTION, JOptionPane.QUESTION_MESSAGE, null, new Object[]{button1, button2}, button1) == JOptionPane.OK_OPTION) { JOptionPane.showMessageDialog(null, "Button1"); } else{ JOptionPane.showMessageDialog(null, "Button2"); }
See above, I always get button2 popup, no matter what.