Swing window doesn't appear after getting input from Console
-
13-11-2019 - |
Question
I have a simple swing window in order to load files.
This appear in the class analyzedLoad
, in a function analyzedloads()
JFileChooser fc = new JFileChooser();
JFrame frame = new JFrame();
int returnVal = fc.showOpenDialog(frame);
frame.dispose();
if (returnVal == JFileChooser.APPROVE_OPTION) {
Where I apply the function without get an input from the user, all fine. But where I get an input from the user, in this way:
int al= 0;
Scanner in = new Scanner(System.in);
System.out.println("for choose file, press 1; for save, press 2");
al= in.nextInt();
if (al== 1){
analyzedLoad.analyzedloads(); // A static function which open the swing window
The window doesn't appear, and the process continue to run, without doing anything.
Thanks.
Solution
Becaue "a scanning operation may block waiting for input," I suspect you're blocking the event dispatch thread. Instead use a File Chooser to obtain a file reference.
OTHER TIPS
Try adding a second mywindow.setVisible(true)
after the console operation.
You might want to try to declaring the analyzeLoad
variable as final
and do something like so:
SwingUtilities.invokeLater(new Runnable()
{
@Override
public void run()
{
analyzedLoad.analyzedloads();
}
}
or since the method is static:
SwingUtilities.invokeLater(new Runnable()
{
@Override
public void run()
{
YourClass.analyzedloads();
}
}
That being said, without more code we can only speculate.