Search code examples
javajoptionpane

How to return to the previous frame with JOption Pane?


So i'm working on a project for school, in which I have created a database with the information for all the chemicals on the periodic table, and upon running the program, the user is prompted to pick between a few options, such as search, sort, etc. Like this:

 String[] options = new String[] {"Search", "Sort", "Add", "Delete", "Exit"};
    int x=JOptionPane.showOptionDialog(null, "Choose one of the following:", "Virtual Periodic        Table", JOptionPane.DEFAULT_OPTION, JOptionPane.PLAIN_MESSAGE,
            null, options, options[0]);

    while (true)
    {
        if (options[x]=="Search")                           
        { 
            searchMenu();  
        }

        else if (options[x]=="Sort")
        {

        }

        else if (options[x]=="Add")
        {

        }

        else if (options[x]=="Delete")
        {

        }

        else if (options[x]=="Exit")
        {
            break;
        }
    }

I am not quite finished, as of right now I am still working on the search option. Anyways, upon clicking the search option, I call a procedure which pops up another menu which prompts the user to specify what they would like to search by, like this:

 String[] options = new String[] {"Name", "Symbol", "Atomic Number", "Atomic Mass", "# of Valence     Electrons", "Go Back"};
    int x=JOptionPane.showOptionDialog(null, "What would you like to search by?", "Virtual  Periodic Table", JOptionPane.DEFAULT_OPTION, JOptionPane.PLAIN_MESSAGE,
            null, options, options[0]);

    if (options[x]=="Name")                           
    { 
        searchByName();
    }

    else if (options[x]=="Symbol")
    {

    }

    else if (options[x]=="Atomic Number")
    {

    }

    else if (options[x]=="Atomic Mass")
    {

    }

    else if (options[x]=="# of Valence Electrons")
    {

    }      
    else if (options[x]=="Go Back")
    {

    }

Now, I am trying to figure out a way in which I can give the user the option to go back to the main menu so they can do something else with the data. For example if i decide to search but then i decide i want to actually sort, I can simply press "Go Back" and it will popup the previous menu, which was the main menu, so I can choose what I want to do next. I have added the button which says "Go Back" but i'm not quite sure how to make it so it actually gives command back to the main method which can popup the main menu again. I tried to put the search menu in a while loop and then making it break out of the loop if they were to click "Go Back" but it didn't no anything but close the entire program.

I'm still fairly new to GUI in Java, anybody know what i can do?


Solution

  • You must put the ShowOptionDialog inside the while loop, so it will be called again when you leave the searchMenu