Reputation: 113
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;
}
}
Upvotes: 1
Views: 1992
Reputation: 9192
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.
Upvotes: 1
Reputation: 4403
The best approach would be to implement the Command
design pattern.
However, if the point is that the triangleRoutines
needs to return to the previous menu, then given the overall current approach, I think making a modification on where the menu is presented would work. I'm not positive that is the OP's question, but I believe so.
See the comments in the example code for additional details.
Not tested, but something like:
private static void triangleRoutines() {
...
// if the program is done, can return null
// if cancel, then return anything but null
return "";
}
Then
do {
// get the menu from the top level
String menu = (String) JOptionPane.showInputDialog(null,"choose","Main menu",JOptionPane.QUESTION_MESSAGE,null,choices,choices[0])
switch (menu){
case "1. Routines for Triangles":
// if cancel, then will return empty string, and then
// re-present the main menu
triangleRoutines(); //one sub menu
break;
...
case "6. Quit":
menu = null;
break;
} //switch
} while (menu != null);
Thus, in the triangleRoutines()
one can "cancel" and it will return an empty String
, which is not null, so it will present the top level menu again. If one wants to exit from the submenu triangleRoutines()
then return null, and the program will exit.
Again, I might have misunderstood the specific question.
Upvotes: 0