Search code examples
javaswingloopsjoptionpane

Returning from the top of the code in Java,How?


is there any other way to return from the top of the code? I already tried using Scanner and it worked but now I want to use YES_NO_OPTION in JOption but i dont have any idea to return from the top of the code using this method.

This is the last part of the program:

int selectedOption = JOptionPane.showConfirmDialog(null,"Continue?","Choose",JOptionPane.YES_NO_OPTION);
if (selectedOption == JOptionPane.YES_OPTION) {
//What should i put here inside the bracket?
}
if (selectedOption == JOptionPane.NO_OPTION) {
JOptionPane.showMessageDialog(null,"Thank you for using");
}

and What should i put in the top of the code to read my Command inside the Bracket? any help will be appriciated.


Solution

  • It seems like you want a loop:

    int selectedOption = JOptionPane.YES_OPTION; // by default
    while (selectedOption == JOptionPane.YES_OPTION) {
        // do your stuff
        selectedOption = 
            JOptionPane.showConfirmDialog(null,"Continue?","Choose",JOptionPane.YES_NO_OPTION);
    }