Open a new JFrame
Asked Answered
D

5

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.

Dallapiccola answered 1/3, 2013 at 21:46 Comment(2)
So what's problem ? just make that JFrame visible when the first calculate button is clicked..!!!Trevelyan
[#1655794 Here is another thread that should answer your question. [1]: https://mcmap.net/q/1160836/-java-swing-multiple-windows-closedMeredeth
B
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()
Boult answered 1/3, 2013 at 22:29 Comment(0)
T
11
JFrame newFrame = new JFrame();
newFrame.setVisible(true);
Take answered 1/3, 2013 at 21:48 Comment(17)
please to wrap setVisible(true); into invokeLaterBoult
:-) this is to your answersBoult
@Take In Java, the GUI-thread (EDT) is not the same as the main thread.Dillman
@mkorbel if the op isn't on the EDT, then both statements need to be placed with in InvokeLater ;)Protection
@Protection only pack(if exists) and setVisisble, welcome on SO :-)Boult
Also it is bad practice to have more than one JFrame within the same swing application, that's why we have JDialog.Dillman
@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 EDTProtection
@Protection slightly disagree (reason why we are here) the same song as with our princessin see point f), I'm still sure that all value and used methods are static modifiers, then important is invoke pack(LayoutManager on EDT) and setVisibleBoult
How about the JInternalFrame.? Won't it be much user friendly and would fulfill what use is looking for.!!?Trevelyan
@mKorbel: He said he wants to do this in a button click handler.Take
@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,Boult
@Take correct, and the button-click handler is executed on the EDT, so your answer is correct +1. Though, I disagree of having multiple JFrames.Dillman
@Take you are right, whatever invoked from Swing Listener is done on EDT,Boult
@Take back to my invokeLater, described SPSS by OP could be hard and long running Object, SwingWorker or invokeAndWait is proper of waysBoult
@Boult 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 |)Protection
@Protection :-) we wrote here about important rulles, but see world is really mad&crazy, everything is possible here, here and here, those examples aren't about following good practices.....Boult
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..Borlase
D
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?.

Dillman answered 1/3, 2013 at 22:1 Comment(1)
For (almost) every rule there is an exception - good to point it out though ;)Protection
H
4

I maybe mis understanding your question but

JFrame frame = new JFrame();
frame.setVisible(true);
Hanukkah answered 1/3, 2013 at 21:50 Comment(2)
not the same issue, see comments to the answer by his majesty @TakeBoult
The question can lead people to mis understanding hahaTherapsid
C
0

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

Conard answered 25/11, 2019 at 10:5 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.