-1

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.

mKorbel
  • 109,525
  • 20
  • 134
  • 319
Adam Sh
  • 8,137
  • 22
  • 60
  • 75

3 Answers3

2

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.

trashgod
  • 203,806
  • 29
  • 246
  • 1,045
1

Try adding a second mywindow.setVisible(true) after the console operation.

ApprenticeHacker
  • 21,351
  • 27
  • 103
  • 153
  • @It doesn't help. I add it to the class analyzedLoad, where my windows is known, and the problem continue. – Adam Sh Feb 28 '12 at 08:15
1

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.

npinti
  • 51,780
  • 5
  • 72
  • 96
  • It doesn't help. Can you tell me plz which more code I need to give? I wrote the relevant part in the two classes. Thanks – Adam Sh Feb 28 '12 at 08:35
  • @AdamSh: Whenever messing around with GUI it is usually good to make a distinction between showing nothing and doing nothing. Did you try to initialize the JFrame to something that is actually visible, maybe the `JFrame` is being opened but it is not visible. I would recommend you first create a visible `JFrame` and then try to load a `JFileChooser` – npinti Feb 28 '12 at 08:41
  • Without the input from the console, everything works fine: The window appear, and the program work well. So I guess that my GUI's defenition is fine. Also, I add fc.setVisible(true); after the define of fc, and It didn't help as well. – Adam Sh Feb 28 '12 at 08:54
  • @AdamSh: The only thing that comes to mind is that the `Scanner` might be hanging your program. Can you try to do a `in.close()` after you get the value from `in.nextInt()`. – npinti Feb 28 '12 at 08:57
  • @AdamSh: Would it be possible to replace the console section with a radio group and a button? – npinti Feb 28 '12 at 09:07