import javax.swing.JOptionPane; public class Example7 { private String optionSelected; public Example7() {} public void showMessageDialog (String message, String title) { JOptionPane.showMessageDialog( null, message, title, JOptionPane.INFORMATION_MESSAGE); } public int showOptionDialog (String message, String title) { return showOptionDialog(message,title,null); } public int showOptionDialog (String message, String title, String _options) { String[] options; if (_options == null) options = "Yes,No".split(","); else options = _options.split(","); int choice = -1; optionSelected = null; try { choice = JOptionPane.showOptionDialog( null, message, title, JOptionPane.DEFAULT_OPTION, JOptionPane.QUESTION_MESSAGE, null, options, options[0]); if (choice >= 0) optionSelected = options[choice]; } catch (Exception e) { JOptionPane.showMessageDialog(null,e, "Exception in showOptionDialog", JOptionPane.WARNING_MESSAGE); } return choice; } public String getOptionSelected () { return this.optionSelected; } public static void main (String arg[]) { Example7 me = new Example7 (); int answer = me.showOptionDialog ( "Two plus Two equals", "Math question", "Zero,One,Two,Three,Four,Five"); System.out.println ("You selected option " + answer + " " + me.getOptionSelected()); me.showMessageDialog ("Thanks for playing",""); System.exit(0); } }