Why not simply
String[] options = new String[] {"Go ahead", "Go back", "Go forward", "close me"};
int option = JOptionPane.showOptionDialog(null, "Title", "Message",
JOptionPane.DEFAULT_OPTION, JOptionPane.PLAIN_MESSAGE,
null, options, options[0]);
if (option != JOptionPane.CLOSED_OPTION) {
System.out.println(options[option]);
} else {
System.out.println("No option selected".);
}
Note that use of an enum for the options will more easily allow use of the state or command design pattern. For example:
import javax.swing.JOptionPane;
public class OptionPaneEgWithEnums {
public static void main(String[] args) {
int option = JOptionPane.showOptionDialog(null, "Title", "Message",
JOptionPane.DEFAULT_OPTION, JOptionPane.PLAIN_MESSAGE, null,
State.values(), State.values()[0]);
if (option == JOptionPane.CLOSED_OPTION) {
// user closed the JOptionPane without selecting
} else {
State state = State.values()[option];
doAction(state);
// code to do something based selected state
}
}
private static void doAction(State state) {
System.out.println("The user has selected to " + state);
}
}
enum State {
AHEAD("Go Ahead"), BACK("Go Back"), FORWARD("Go Forward"), CLOSE("Close Me");
private State(String text) {
this.text = text;
}
private String text;
public String getText() {
return text;
}
@Override
public String toString() {
return text;
}
}