permaculture playing cards
The moose likes Swing / AWT / SWT and the fly likes Getting data for display in a new JFrame Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of Java Interview Guide this week in the Jobs Discussion forum!
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "Getting data for display in a new JFrame" Watch "Getting data for display in a new JFrame" New topic

Getting data for display in a new JFrame

Ruben Matthews

Joined: Jan 05, 2009
Posts: 26

I am learning Swing from the web, which is obviously not a good idea with such a big topic. I ran into the following simple question:

If I spawn a new JFrame from another JFrame, for example from a menu item, where do I load the data, in my case some text from a file, that is needed for display in the new frame?

Chris Nash

Joined: May 20, 2009
Posts: 28
How about the constructor?
Maneesh Godbole
Saloon Keeper

Joined: Jul 26, 2007
Posts: 10836

Why do you want to spawn a new frame from an existing frame? It is a good idea to use Dialogs in such scenarios. Check out JDialog and JOptionPane API. The JOPtionPane#showXXXDialog accepts an Object as the dialog content. So you can even considering passing a JPanel with other controls as its children

How to load data is a design question. I usually define a sequence in my constructor, where the components are initialized, then laid out on the parent container using appropriate layouts. If I plan to use the same UI to display details of multiple objects, I have a method which accepts the objects, and changes the values inside the components. That way I do not need to initialize and re-layout the components every time.

[How to ask questions] [Donate a pint, save a life!] [Onff-turn it on!]
Michael Dunn
Ranch Hand

Joined: Jun 09, 2003
Posts: 4632
a lot depends on the purpose of frame2, and its necessity
i.e. many (most) similar posts (re frame2) end up changing
their design to either a CardLayout, or use of a modal JDialog

Ruben Matthews

Joined: Jan 05, 2009
Posts: 26
I have used the constructor. Thanks Maneesh for the remarks. I need to dig deeper into this subject, because right now I am doing what works instead what is the intended use of Swing
I agree. Here's the link:
subject: Getting data for display in a new JFrame
It's not a secret anymore!