I've got dialog in my project with 3 buttons and I want them to execute some code. I know that these methods return integer (different for every button) and i can use "if" instruction to check which button was pressed and to perform specific action. It works but at first it closes dialog which is not what I want. I'd like to close it only via 3rd button or [x].
public void popup(){
JButton[] buttons = {new JButton("Save"),new JButton("Re-draw"),new JButton("Close")};
if(results.size()==0){
JOptionPane.showMessageDialog(frame, "Please fill all fields", "Error", JOptionPane.PLAIN_MESSAGE);
}
else{
JList<String> resultsList = new JList(results.toArray(new String[results.size()]));
int result = JOptionPane.showOptionDialog(frame, resultsList, "Results",JOptionPane.DEFAULT_OPTION, JOptionPane.PLAIN_MESSAGE, null, buttons, buttons[0]);
}
});
System.out.println(result);
if (result == 0){
/*saving results*/
}
else if(result == 1){
draw();
}
As i said, it works, but pressing any of button results in closing dialog. I dont know wheter its possible disable automatic dialog closing for buttons "Save" and "Redraw"?
Read the section from the Swing tutorial on Stopping Automatic Dialog Closing .
The other option of course if to just create your own custom JDialog that implements the functionality you desire.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.