Reputation: 57
I recently modified my program and I am having trouble figuring out why it does not wait for user input when I call another class that uses JFrame (in this class the user selects his/her choice from a dropdown menu (ComboBox)). It is worth noting that the program was running fine before I converted to GUI, when I was using the scanner.nextLine() function.
When the variable Airline has an undefined value, the program is in a While Loop, and until the user chooses a valid response from the ComboBox the program will exit the While Loop. The problem is that program seems to infinitely loop and will not stop for user input when the UndefinedLine Class is called. Do you guys know how I can fix this? Back when I was using the scanner.nextLine funcion it worked really well. What would be the equivalent of this function after modifying the program to GUI? Please find code below:
while (ExitLoop == false){
switch (Airline) { //Variable Airline
case "AA": // if Airline variable is "AA"
ExitLoop = true;
AA();
break;
case "UA":
ExitLoop = true;
UA();
break;
default:
UndefinedLine UDLobject = new UndefinedLine();
UDLobject.main(args); //Calls class UndefinedLine, which is a JFrame Message with a ComboBox, for which the user chooses the correct Airline and based on the user's response the variable Airline is assigned.
UDLobject.Airline= Airline;//Assigns value for variable Airline from Class UndefinedLine after user has selected the choice
break;
}
}
Thanks, your help will be much appreciated.
Upvotes: 0
Views: 103
Reputation:
JFrame
is never modal. This means, if you create a new frame the program won't wait until that frame is closed before the normal program continues. This would require a JDialog
. And calling the main
-method provided by another class isn't creating a new process either and thus the program won't wait for user-input. So far for what isn't possible.
Solutions:
JDialog
. This class is modal. This means after making the dialog visible the program waits until the dialog is closed.wait()
and notify()
. Note though that calling wait()
within the event-queue will block the event-queue and thus most likely the flow of your program.Preferable solution would be a JDialog
, implementing the lock on your own is neither recommended nor necassary, unless you want to either experimentate or have a really complex problem that can't be solved using JDialog
.
Upvotes: 1