How do I manage to make my first messagebox to stay up?

I was wondering how I manage to ask a question in a messagebox....and if the question is wrong a nother messagebox opens. When I click ok on the second box I want to comeback to the first one. Right now everything just ends.

See code
int response = JOptionPane.showOptionDialog(
          null,
          connectionPanel,
          ConnectTitle,
          JOptionPane.OK_CANCEL_OPTION,
          JOptionPane.INFORMATION_MESSAGE,
          null,
          ConnectOptionNames,
          ConnectOptionNames[0]);
 
swithc(.... bla bla)
 case: 1  login();
 
 
public void login(){
JOptionPane.showMessageDialog(this, "name does not exist");
}

Open in new window

MickeysAsked:
Who is Participating?
 
objectsConnect With a Mentor Commented:
     int response = -1;

      while (response!=1) {
        response = JOptionPane.showOptionDialog(
          null,
          connectionPanel,
          ConnectTitle,
          JOptionPane.OK_CANCEL_OPTION,
          JOptionPane.INFORMATION_MESSAGE,
          null,
          ConnectOptionNames,
          ConnectOptionNames[0]);
         
 
      switch (response) {
                case 0:
                        System.out.println("0");
                    break;
                case 1:
                    login();                    
                    break;
               
                case 2:
                case -1:
                    //... Both the quit button (3) and the close box(-1) handled here.
                    // System.exit(0);     // It would be better to exit loop, but...
                    response = 1;
                default:
                    //... If we get here, something is wrong.  Defensive programming.
                    JOptionPane.showMessageDialog(null, "Unexpected response " + response);
            }
        }
    }

0
 
CEHJCommented:
Set it in a loop that only exits when the answer is correct
0
 
objectsCommented:
while (response!= desiredResponse)
{
int response = JOptionPane.showOptionDialog(
          null,
          connectionPanel,
          ConnectTitle,
          JOptionPane.OK_CANCEL_OPTION,
          JOptionPane.INFORMATION_MESSAGE,
          null,
          ConnectOptionNames,
          ConnectOptionNames[0]);
 
swithc(.... bla bla)
 case: 1  login();

0
The 14th Annual Expert Award Winners

The results are in! Meet the top members of our 2017 Expert Awards. Congratulations to all who qualified!

 
objectsCommented:
should be:

int response = -1;
while (response!= desiredResponse)
{
   response = JOptionPane.showOptionDialog(


give me a yell if you don't understand

0
 
objectsCommented:
I actually have a full example from one of my students but can't find it right now.
0
 
MickeysAuthor Commented:
Where?
      // Connect or quit
      int response = JOptionPane.showOptionDialog(
          null,
          connectionPanel,
          ConnectTitle,
          JOptionPane.OK_CANCEL_OPTION,
          JOptionPane.INFORMATION_MESSAGE,
          null,
          ConnectOptionNames,
          ConnectOptionNames[0]);
          
 
      switch (response) {
                case 0: 
                	System.out.println("0");
                    break;
                case 1:
                    login();                    
                    break;
                
                case 2:
                case -1:
                    //... Both the quit button (3) and the close box(-1) handled here.
                    System.exit(0);     // It would be better to exit loop, but...
                default:
                    //... If we get here, something is wrong.  Defensive programming.
                    JOptionPane.showMessageDialog(null, "Unexpected response " + response);
            }
    
    }
      
      public void login()
      {  	  
    	JOptionPane.showMessageDialog(this, "No name");
      }

Open in new window

0
 
CEHJCommented:
Set it before your switch
0
 
CEHJCommented:
int response = Integer.MIN_VALUE;
while (response < 0 && response > 2) {
...
}
0
 
CEHJCommented:
Glad i could help
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.