3

My swing app has a main window which has sever buttons. This JFrame is launched in the EDT...

    EventQueue.invokeLater(new Runnable() {
        public void run() {
            JFrame server = new JFrame();
            server.setVisible(true);
            server.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);

                            //Do other stuff here
        }
    });

Each button can launch a separate JFrame.

I am not sure if I have to start a new EDT thread to launch each of these new windows? Thanks.

mKorbel
  • 109,525
  • 20
  • 134
  • 319
sachinrahulsourav
  • 742
  • 1
  • 7
  • 16
  • possible duplicate of [Why do people run Java GUI's on the Event Queue](http://stackoverflow.com/questions/3018165/why-do-people-run-java-guis-on-the-event-queue) – Anonymous Apr 11 '12 at 19:52
  • Nope. Not a duplicate, please read my question carefully and completely, not just the title. – sachinrahulsourav Apr 11 '12 at 19:54
  • *"Each button can launch a separate JFrame."* (Shudder) See [The Use of Multiple JFrames, Good/Bad Practice?](http://stackoverflow.com/a/9554657/418556) – Andrew Thompson Apr 12 '12 at 08:38
  • _if I have to start a new EDT thread_ - there is exactly **one** EDT :-) – kleopatra Apr 12 '12 at 09:34

3 Answers3

4

Typically when you are in a button handler (ie actionlistener) it is the EDT that is calling this. You can check this using SwingUtilities.isEventDispatchThread

ControlAltDel
  • 33,923
  • 10
  • 53
  • 80
3

There is no problem - each action handler is run on the EDT. So if you open new JFrame from such place there will be no problem.

If you were doing some work (e.g. calculations) in a different thread, then it is fine, but all the work that touches UI has to be done on the EDT. That's why we have SwingWorker and invokeLater.

Anonymous
  • 18,162
  • 2
  • 41
  • 64
2

my answers are

but

  • not, you can prepare JFrame with its contents before, but for EDT there are two the most impotant methods,

    1. JFrame#pack() // finalize & calculate used LayoutManager

    2. JFrame#setVisible(true) // display Container on the screen

  • Each button can launch a separate JFrame. don't do that, use CardLayout instead, for switching betweens views, if you needed popup window then to create only one JFrame and another Container could be JDialog only

mKorbel
  • 109,525
  • 20
  • 134
  • 319