10

I have a main JFrame that has all kinds of panels in it for different functions and people can calculate things in them. I want to open a new JFrame when the user hits the first calculate button and serve as a Output window (Simlar to SPSS output windows if you are familiar with them).

The New JFrame will be completely separate and will have its own menu bar ... A simple JDialog is not the way to go here.

tckmn
  • 57,719
  • 27
  • 114
  • 156
Killerpixler
  • 4,200
  • 11
  • 42
  • 82
  • So what's problem ? just make that `JFrame` visible when the first calculate button is clicked..!!! – Vishal K Mar 01 '13 at 21:48
  • [http://stackoverflow.com/questions/1655294/java-swing-multiple-windows][1] Here is another thread that should answer your question. [1]: http://stackoverflow.com/questions/1655294/java-swing-multiple-windows – Joe Mar 01 '13 at 21:48

5 Answers5

13
  • can't resist, simple disagree with answers JFrame frame = new JFrame(); and frame.setVisible(true);

I want to open a new JFrame when the user hits the first calculate button and serve as a Output window (Simlar to SPSS output windows if you are familiar with them).

  • don't do that, create only two JFrames, reuse 2nd. JFrame by using getContentPane.removeAll(), for another actions from JButton

  • then all lifecycle will be only about setVisible(true) / setVisible(false)

  • change DefaultCloseOperations to HIDE_ON_CLOSE

The New JFrame will be completely separate and will have its own menu bar. A simple JDialog is not the way to go here.

  • whats wrong with JDialog, only one button in the Toolbar in compare with three buttons in JFrame, simple disagree,

Output window (Simlar to SPSS output windows if you are familiar with them).

  • use SwingWorker or Runnable#Thread (required wrap into invokeLater) for get value for JComponents placed into JDialog, if all changes are done call JDialog.setVisible(true) wrapped into invokeLater()
Community
  • 1
  • 1
mKorbel
  • 109,525
  • 20
  • 134
  • 319
11
JFrame newFrame = new JFrame();
newFrame.setVisible(true);
SLaks
  • 868,454
  • 176
  • 1,908
  • 1,964
  • 3
    please to wrap setVisible(true); into invokeLater – mKorbel Mar 01 '13 at 21:48
  • :-) this is to your answers – mKorbel Mar 01 '13 at 21:50
  • @SLaks In Java, the GUI-thread (EDT) is not the same as the main thread. – Eng.Fouad Mar 01 '13 at 21:50
  • @mkorbel if the op isn't on the EDT, then both statements need to be placed with in InvokeLater ;) – MadProgrammer Mar 01 '13 at 21:51
  • @MadProgrammer only pack(if exists) and setVisisble, welcome on SO :-) – mKorbel Mar 01 '13 at 21:52
  • 3
    Also it is bad practice to have more than one `JFrame` within the same swing application, that's why we have `JDialog`. – Eng.Fouad Mar 01 '13 at 21:54
  • @mkorbel there's no guarantee to when a UI component might try and interact with other parts of UI, always safer to create and manipulate on the EDT – MadProgrammer Mar 01 '13 at 21:56
  • @MadProgrammer slightly disagree (reason why we are here) the same [song as with our princessin see point f)](http://stackoverflow.com/a/15137708/714968), I'm still sure that all value and used methods are static modifiers, then important is invoke pack(LayoutManager on EDT) and setVisible – mKorbel Mar 01 '13 at 22:00
  • How about the JInternalFrame.? Won't it be much user friendly and would fulfill what use is looking for.!!? – Vishal K Mar 01 '13 at 22:03
  • @mKorbel: He said he wants to do this in a button click handler. – SLaks Mar 01 '13 at 22:06
  • @Vishal K (my view) by default multiple instances are required for multiple screens, this idea is correct, but not all methods are accessible betweens two or more JFrames as is decribed in API, – mKorbel Mar 01 '13 at 22:07
  • @SLaks correct, and the button-click handler is executed on the EDT, so your answer is correct +1. Though, I disagree of having multiple `JFrame`s. – Eng.Fouad Mar 01 '13 at 22:09
  • @SLaks you are right, whatever invoked from Swing Listener is done on EDT, – mKorbel Mar 01 '13 at 22:09
  • @SLaks back to my invokeLater, described [SPSS](http://www-01.ibm.com/software/analytics/spss/) by OP could be hard and long running Object, SwingWorker or invokeAndWait is proper of ways – mKorbel Mar 01 '13 at 22:14
  • @mKorbel I'm happy to admit that was more the 5 years ago I read the article and I may be taking it to literally, but my understand is, there is no guarantee when any window will attach itself to a native peer and is "safer" not to assume, especially between implementations, so rather the risking, I simply fall under the "create it all on the EDT" rule :) - ps I'm always happy for an argument - it helps me learn |) – MadProgrammer Mar 01 '13 at 22:41
  • @MadProgrammer :-) we wrote here about important rulles, but see world is really mad&crazy, everything is possible [here](http://stackoverflow.com/a/7442529/714968), [here](http://stackoverflow.com/a/14988796/714968) and [here](http://stackoverflow.com/a/9301453/714968), those examples aren't about following good practices..... – mKorbel Mar 01 '13 at 22:51
  • There is no problem when we use so many JFrame instead of JDialog.. The most important is user accept our program. That is the main goal of software development.. – toha Mar 17 '19 at 08:19
5

Never use more than one JFrame within a Swing application. Use JDialog for extra windows instead.

See The Use of Multiple JFrames, Good/Bad Practice?.

Community
  • 1
  • 1
Eng.Fouad
  • 115,165
  • 71
  • 313
  • 417
4

I maybe mis understanding your question but

JFrame frame = new JFrame();
frame.setVisible(true);
clicky
  • 865
  • 2
  • 14
  • 31
0

I used code JFrame frame = new JFrame(); frame.setVisible(true);. This block of code just do empty window.

HaiCNCS
  • 5
  • 1
  • 5