File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Swing / AWT / SWT and the fly likes Swing: Adding components to containers Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "Swing: Adding components to containers" Watch "Swing: Adding components to containers" New topic
Author

Swing: Adding components to containers

Joel Black
Greenhorn

Joined: Apr 22, 2005
Posts: 2
Newbie question: I'm working with a few books containing examples of Swing use...different methods are used, but basis for choice is not explained. It'd be great if someone could explain why one or the other is better:

Some examples are like this, adding components directly to a JFrame:

public class foo extends JFrame{
...make some components...
Container con = getContentPane();
con.setLayout(new FlowLayout());
add[components];
...

Whereas some are like this, setting a JPanel as the content pane:

Some examples are like this, adding components directly to a JFrame:

public class bar extends JFrame{
...make some components...
JPanel pan = new JPanel();
pan.add[components];
add(pan);
...

I understand the need to change the layout from default when using the JFrame directly (at least if you want it to act like a JPanel)--it's the choice of creating the JPanel or not that I'm not clear on. Why would I use one method over the other?

Thanks!
Joe Ess
Bartender

Joined: Oct 29, 2001
Posts: 8708
    
    6

Originally posted by Joel Black:
it's the choice of creating the JPanel or not that I'm not clear on. Why would I use one method over the other?


Once you graduate to GUI's with more than a couple of components it's sometimes necessary to group a number of them together in a JPanel. It's not uncommon to have JPanels with components nested a couple deep in order to get a layout to work. It is also common to subclass of JPanel and a number of components, their models and events to make a reusable component.
One other source of inconsistency in your examples, before JDK 1.5, you HAD to add components to a JFrame's ContentPane. Calling JFrame.add() directly threw an exception. Some of your examples may be written for JDK 1.5 and can get away with calling JFrame.add().


"blabbing like a narcissistic fool with a superiority complex" ~ N.A.
[How To Ask Questions On JavaRanch]
Ernest Friedman-Hill
author and iconoclast
Marshal

Joined: Jul 08, 2003
Posts: 24166
    
  30

Hi,

Welcome to JavaRanch!

We've got a forum devoted to Swing and AWT here. I'm going to move this thread over there for any further discussion.


[Jess in Action][AskingGoodQuestions]
Joel Black
Greenhorn

Joined: Apr 22, 2005
Posts: 2
Thanks, guys--I appreciate the info!
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Swing: Adding components to containers
 
Similar Threads
Ugh...so confused.
Matisse designer_problem with displaying drawn circle at JPanel/also animation problem
ClassCastException: sun.plugin.viewer.frame.IExplorerEmbeddedFrame to ViewerFrame
JList in JScrollPane
extending JFrame vs having JFrame as a field