Java Dialog Box - Code Keeps on going....I need it to stop until someone clicks OK

When this dialog box in my code is triggered, the pop-up shows up, but users can ignore it, hit enter a bunch of times, and it seems the code continues on, leaving the "OK" button showing.  I don't want to the code to proceed further until someone clicks OK.  How? (in Java)

if (animal.contains("*")) {
                 
                  // Remvoes *s from alert
                  alert = animal.replace("*", "");
                 
                  frame.setAlwaysOnTop(true);
                 
                  JOptionPane.showMessageDialog(frame, alert);
                 
                  animal = inFS.nextLine();
               }
               // Else just read next line
               else {
                 
                  animal = inFS.nextLine();
               }
LVL 1
NAMEWITHELD12Asked:
Who is Participating?
 
CEHJCommented:
That seems strange since JOptionPane is modal by default:

All dialogs are modal. Each showXxxDialog method blocks the caller until the user's interaction is complete.
(JOptionPane API docs)

Can you post a self-contained runnable example that demonstrates the problem?
0
 
NAMEWITHELD12Author Commented:
I should probably be a bit more clear in my question, actually, now that I realize it.  The loop that prints text stops until the "OK" is clicked.  That part works.

But if I go back and click in the text area and hit enter a bunch of times to produce blank spaces, and THEN I click OK, those blank lines are read in to my scnr.nextLine() producing some really unexpected results.
0
 
CEHJCommented:
You really need to produce runnable code
0
 
NAMEWITHELD12Author Commented:
I was able to fix it by just eliminating my friendly "Please enter an option." statement so that a user could hit "Enter" as many times as they wanted and the scanner.nextLine() after the pop-up would not accept the "Enter" as its entry and crash the program.  Thanks for the details on modal!
0
 
CEHJCommented:
:)
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.