Question

I need to popup some one dialog and get the user confirmation . and I want this dailog should be always top , till user select yes or No option . below is the program I wrote , but now how to get which option has user selected .. i.e Yes or NO ..?

JFrame parent = new JFrame();
JOptionPane optionPane = new JOptionPane("DO you want to continue?",         JOptionPane.QUESTION_MESSAGE, JOptionPane.YES_NO_OPTION);
JDialog  dialog = null ;

dialog = optionPane.createDialog(parent, "Next Job process");
dialog.setAlwaysOnTop(true);
dialog.setVisible(true);
Was it helpful?

Solution 2

Did you read JOptionPane's documentation?

There are two ways to use it. One way is to use the various static methods that already return the user's choice or input.

The other way is a direct use (mentioned in the documentation). Once you created a JOptionPane, you can configure it with various setX methods. Then you retrieve a JDialog instance, as you already did. After that dialog is close, you can retrieve the user's choice or input with

optionPane.getValue()

or

optionPane.getInputValue()

OTHER TIPS

JOptionPane.YES_NO_OPTION return int value

you may try:

int selectedOption = JOptionPane.showConfirmDialog(null, 
                       "Do you want to continue?", 
                       null, 
                       JOptionPane.YES_NO_OPTION); 

if (selectedOption == JOptionPane.YES_OPTION) {
    //do something
}
else{ //user choose 'No'
    //do something
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top