?
Solved

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

Posted on 2009-04-22
9
Medium Priority
?
254 Views
Last Modified: 2012-05-06
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

0
Comment
Question by:Mickeys
  • 4
  • 4
9 Comments
 
LVL 86

Expert Comment

by:CEHJ
ID: 24203594
Set it in a loop that only exits when the answer is correct
0
 
LVL 92

Expert Comment

by:objects
ID: 24203602
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
 
LVL 92

Expert Comment

by:objects
ID: 24203612
should be:

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


give me a yell if you don't understand

0
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
LVL 92

Expert Comment

by:objects
ID: 24203616
I actually have a full example from one of my students but can't find it right now.
0
 

Author Comment

by:Mickeys
ID: 24203628
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
 
LVL 92

Accepted Solution

by:
objects earned 2000 total points
ID: 24203668
     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
 
LVL 86

Expert Comment

by:CEHJ
ID: 24203670
Set it before your switch
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 24203692
int response = Integer.MIN_VALUE;
while (response < 0 && response > 2) {
...
}
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 24203869
Glad i could help
0

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
In this post we will learn different types of Android Layout and some basics of an Android App.
Viewers will learn about if statements in Java and their use The if statement: The condition required to create an if statement: Variations of if statements: An example using if statements:
This video teaches viewers about errors in exception handling.
Suggested Courses
Course of the Month14 days, 15 hours left to enroll

839 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question