Link to home
Start Free TrialLog in
Avatar of Mickeys
MickeysFlag for Sweden

asked on

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

Avatar of CEHJ
CEHJ
Flag of United Kingdom of Great Britain and Northern Ireland image

Set it in a loop that only exits when the answer is correct
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();

should be:

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


give me a yell if you don't understand

I actually have a full example from one of my students but can't find it right now.
Avatar of Mickeys

ASKER

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

ASKER CERTIFIED SOLUTION
Avatar of Mick Barry
Mick Barry
Flag of Australia image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Set it before your switch
int response = Integer.MIN_VALUE;
while (response < 0 && response > 2) {
...
}
Glad i could help