sorry if this has already been asked but I can't find it anywhere. I made a custom JOptionPane
with a JSlider
. It does what it is supposed to do, but how can I check if the pressed ok or cancel? Because right now it does it anyway.
Code:
JOptionPane optionPane = new JOptionPane();
JSlider slider = getSlider(optionPane);
slider.setValue(value);
optionPane.setMessage(new Object[]{"Select a value: ", slider});
optionPane.setMessageType(JOptionPane.QUESTION_MESSAGE);
optionPane.setOptionType(JOptionPane.OK_CANCEL_OPTION);
optionPane.createDialog(parent, title).setVisible(true);
// get return value of optionPane
I've gone through the methods and didn't find it, that doesn't mean; howerever, that it's not there.
Any help is appreciated. Thanks!
The documentation has a nice example for the way you are creating the dialog.
A short snippet of the documentation:
JDialog dialog = pane.createDialog(parentComponent, title);
dialog.show();
Object selectedValue = pane.getValue();
if(selectedValue == null)
return CLOSED_OPTION;
//If there is not an array of option buttons:
if(options == null) {
if(selectedValue instanceof Integer)
return ((Integer)selectedValue).intValue();
return CLOSED_OPTION;
}
Looks like the pane.getValue()
after you show()
the dialog will have the resulting value.
You may wish to restructure your code to use the more easily usable showOptionDialog
method as described in this related question.