JOptionPane Passing Custom Buttons

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.

+4
source share
3 answers

In the example I linked to the previous question, the buttons use the JOptionPane#setValue to set the return value. This allows you to continue to use the API as usual, giving you customization after.

  final JButton okay = new JButton("Ok"); okay.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); } }); 

Meet disable the ok button on JOptionPane.dialog until user gives login

Updated

I returned the code and fixed the actionPerformed methods actionPerformed that it could return a valid value ...

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

The value returned by the index of the value in the options array (last parameter)

So for example ...

 int value = JOptionPane.showOptionDialog( null, field, "Get", JOptionPane.YES_NO_OPTION, JOptionPane.QUESTION_MESSAGE, null, new Object[]{okay, cancel}, okay); 

If the user clicks the OK button, the return value will be 0 , or if they select the cancel button, it will be 1

+10
source

If you need this complex behavior, consider creating your own JDialog, and then show it in a modal way.

If you need to use JOptionPane, you can do this by extracting JDialog and recursive iteration through your components until you find the one you want to disable and disable:

 import java.awt.Component; import java.awt.Container; import javax.swing.*; public class Foo2 { public static void main(String[] args) { SwingUtilities.invokeLater(new Runnable() { public void run() { doRun(); } }); } public static void doRun() { String[] options = {"Button 1", "Button 2", "Button 3"}; JOptionPane myOptionPane = new JOptionPane("Heres a test message", JOptionPane.QUESTION_MESSAGE, JOptionPane.YES_NO_OPTION, null, options, options[2]); JDialog myDialog = myOptionPane.createDialog(null, "My Test"); myDialog.setModal(true); inactivateOption(myDialog, options[1]); myDialog.setVisible(true); Object result = myOptionPane.getValue(); // Note: result might be null if the option is cancelled System.out.println("result: " + result); System.exit(0); // to stop Swing event thread } private static void inactivateOption(Container container, String text) { Component[] comps = container.getComponents(); for (Component comp : comps) { if (comp instanceof AbstractButton) { AbstractButton btn = (AbstractButton) comp; if (btn.getActionCommand().equals(text)) { btn.setEnabled(false); return; } } else if (comp instanceof Container) { inactivateOption((Container) comp, text); } } } } 

However, for myself, I just created JDialog.

+3
source

You do not need to explicitly specify your buttons.

 int result = JOptionPane.showOptionDialog(this, "Are you sure you want to...?", "Title", JOptionPane.YES_NO_OPTION, JOptionPane.QUESTION_MESSAGE, null, new String[] { "Yes", "No" }, JOptionPane.NO_OPTION); if (result == JOptionPane.YES_OPTION) { ... } 
+2
source

All Articles