Win a copy of Think Java: How to Think Like a Computer Scientist this week in the Java in General forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Getting data for display in a new JFrame

 
Ruben Matthews
Greenhorn
Posts: 26
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello,

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
Greenhorn
Posts: 28
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
How about the constructor?
 
Maneesh Godbole
Saloon Keeper
Posts: 11027
12
Android Eclipse IDE Google Web Toolkit Java Mac Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Michael Dunn
Ranch Hand
Posts: 4632
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Greenhorn
Posts: 26
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic