Reputation: 17
I need help with JOptionPane in Java. When I click OK everything is OK. :) But when I click on x its same like i clicked on OK. How can I solve this. When I click on x I wanna to close app. Here is my code.
import javax.swing.JList;
import javax.swing.JOptionPane;
public class Main {
@SuppressWarnings("unchecked")
public static void main(String[] args) {
@SuppressWarnings("rawtypes")
JList choise = new JList(new String[] { "Enter the complaints",
"View complaints" });
JOptionPane.showMessageDialog(null, choise, "Multi-Select Example",
JOptionPane.PLAIN_MESSAGE);
if (choise.getSelectedIndex() == 0) {
new ComplaintsApp();
} else if(choise.getSelectedIndex() == 1 && JOptionPane.OK_OPTION > 0){
new ComplaintsView();
}
}
}
Upvotes: 1
Views: 150
Reputation: 285405
Use a different JOptionPane, one that returns to the program an indication of which button was pressed: the JOptionPane.showConfirmDialog(...)
:
import javax.swing.JList;
import javax.swing.JOptionPane;
import javax.swing.JScrollPane;
public class Main {
public static void main(String[] args) {
JList<String> choice = new JList<>(new String[] {
"Enter the complaints", "View complaints" });
int result = JOptionPane.showConfirmDialog(null,
new JScrollPane(choice), "Multi-select Example",
JOptionPane.OK_CANCEL_OPTION, JOptionPane.PLAIN_MESSAGE);
if (result != JOptionPane.OK_OPTION) {
// they didn't press OK
return;
}
if (choice.getSelectedIndex() == 0) {
System.out.println("Complaints App");
} else if (choice.getSelectedIndex() == 1) {
System.out.println("Complaints View");
}
}
}
Upvotes: 2