I'm making a method wherein it has several choices and each of those choices , once selected, need to have the "cancel" button go back to the main menu.
Main Menu:
public static void start(){
String[] choices = {"1. Routines for Triangles","2. Routines for Temperatures","3. Routines for Geometric Figures","4. String Manipulations",
"5. Miscellaneous Simple Games.","6. Quit"};
String menu = (String) JOptionPane.showInputDialog(null,"choose","Main menu",JOptionPane.QUESTION_MESSAGE,null,choices,choices[0])
while (menu != null){
switch (menu){
case "1. Routines for Triangles":
triangleRoutines(); //one sub menu
break;
case "2. Routines for Temperatures":
break;
case "3. Routines for Geometric Figures":
break;
case "4. String Manipulations":
break;
case "5. Miscellaneous Simple Games.":
break;
case "6. Quit":
break;
}
}
}
A sub menu:
private static void triangleRoutines(){
String[] stringArray = {"Determine the Type of Triangle", "Determine a Valid Triangle", "Determine the Area of the Sides of a Triangle", "Determine a Pythagorean Triple",
};
String reply = (String) JOptionPane.showInputDialog(null, "Choose what you want to do today", "Triangle Processes", JOptionPane.QUESTION_MESSAGE,
null, stringArray, stringArray[0]);
switch (reply){
case "Determine the Type of Triangle":
break;
case "Determine a Valid Triangle":
break;
case "Determine the Area of the Sides of a Triangle":
break;
case "Determine a Pythagorean Triple":
break;
}
}
When a JOptionPane.showInputDialog() dialog is Canceled or Closed without a selection being made then a null is returned. you need to handle this null and act upon it accordingly but you will need to decide what you want to do when it does happen. Close the application, re-display the Main Menu dialog and force the Quit menu option to be selected, etc.
To always keep your Main Menu available place it within the while loop, for example:
String[] choices = {"1. Routines for Triangles", "2. Routines for Temperatures",
"3. Routines for Geometric Figures", "4. String Manipulations",
"5. Miscellaneous Simple Games.", "6. Quit"};
String menu = "";
while (!menu.equals("6. Quit")) {
menu = (String) JOptionPane.showInputDialog(null, "<html>Choose A Menu Item:<br><br></html>", "Main Menu",
JOptionPane.QUESTION_MESSAGE, null, choices, choices[0]);
if (menu == null) {
//continue; //uncomment and delete line below to force menu item 6 to be selected to quit Main Menu.
break; //Quit the Main Menu
}
switch (menu) {
case "1. Routines for Triangles":
triangleRoutines(); //one sub menu
break;
case "2. Routines for Temperatures":
break;
case "3. Routines for Geometric Figures":
break;
case "4. String Manipulations":
break;
case "5. Miscellaneous Simple Games.":
break;
case "6. Quit":
// This case is not really required unless you want to
// do some cleanup of sorts before quiting.
break;
}
}
Utilize the same technique for your methods which contain sub-menus but add a menu option to: Return To Main Menu
and perhaps one more additional option to: Quit Application
if desired.