Note that there are some explanatory texts on larger screens.

plurals
  1. POJOptionPane Passing Custom Buttons
    primarykey
    data
    text
    <p>I'm trying to get the value returned by custom buttons passed to JOptionPane. However the buttons I pass don't return a value at all. Only when the exit button is pressed is a value of -1 returned. I need this because I am changing the properties of the buttons enabled or disabled. I assume I need the buttons to return some information to the JOptionPane in some way. Any idea?</p> <pre><code> 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); </code></pre> <p>Nb This is related to my previous question - <a href="https://stackoverflow.com/questions/14576196/joptionpane-grey-out-one-button">JOptionPane Grey Out One Button</a></p> <p>I tried setting the value of the buttons like you said but they never return OK or CANCEL.</p> <p>Whenever checking the value of the buttons, they never return the value I set them too. </p> <pre><code> 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"); } </code></pre> <p>See above, always I get the button2 popup no matter what.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload