JAVA: Ways to fill a Frame. add(), setContentPane(), getContentPane()
Asked Answered
L

4

14

I found three ways to fill my JFrame frame = new JFrame("...") createContentPanel returns a JPanel and createToolBar returns a ToolBar.

frame.add(this.createToolBar(), BorderLayout.PAGE_START); //this works and puts the ToolBar above and the ContentPanel under it<br>
frame.add(this.createContentPanel(), BorderLayout.CENTER);

frame.setContentPane(this.createContentPanel()); //this lets the JToolBar hover over the ContentPanel
frame.getContentPane().add(this.createToolBar()); 

frame.getContentPane().add(this.createContentPanel()); //this only puts the last one into the JFrame
frame.getContentPane().add(this.createToolBar());

And now I am wondering why should i use the getContentPane()/setContentPane() method if i could just use a simple frame.add(...) to fill my frame.

Litch answered 26/6, 2011 at 21:27 Comment(0)
S
9

You are right that it doesn't matter which you use (JFrame#add(...) vs. JFrame#getContentPane().add(...)) since they both essentially call the same code, however there will be times in the future when you'll need access to the contentPane itself, such as if you want to change its border, set its background color or determine its dimensions, and so you'll likely use getContentPane() at some point, and thus getting to know it and be familiar with it would be helpful.

Skimmer answered 26/6, 2011 at 21:31 Comment(2)
Thanks for your response. But there is a difference between these three variants. I could guess that the problem between the secound and the third one is, that i first net to set a ContentPane, before I can add something serious. But when i compare the first and the third it is a huge difference.Litch
@froehli: Sure, the difference between the two is in the first you're taking the contentPane's layout into consideration, something that should always be done! Note that the contentPane uses BorderLayout by default, but if you set the contentPane via setContentPane, then you are responsible for setting the contentPane's layout to whatever would work best for you.Skimmer
R
2

//this only puts the last one into the JFrame

You need to understand how layout managers work. The default content pane is a JPanel that uses a BorderLayout. When you add a component and don't specify a constraint, then it defaults to the CENTER. However you can only has a single component in the center so the layout manager only knows about the last one added. When the layout manager is invoked it sets the size() and location() of that component. The other component has a size of 0, so it is never painted.

Ralli answered 26/6, 2011 at 21:53 Comment(0)
A
2

In Java 1.6, you can just use the add method of JFrame: http://download.oracle.com/javase/6/docs/api/javax/swing/JFrame.html (It will be delegated to the contentPane.)

Amu answered 17/8, 2011 at 20:57 Comment(0)
J
0

http://download.oracle.com/javase/1.4.2/docs/api/javax/swing/JFrame.html

Which says:

The JFrame class is slightly incompatible with Frame. Like all other JFC/Swing top-level containers, a JFrame contains a JRootPane as its only child. The content pane provided by the root pane should, as a rule, contain all the non-menu components displayed by the JFrame. This is different from the AWT Frame case. For example, to add a child to an AWT frame you'd write:

   frame.add(child);   

However using JFrame you need to add the child to the JFrame's content pane instead:

   frame.getContentPane().add(child);  

The same is true for setting layout managers, removing components, listing children, and so on. All these methods should normally be sent to the content pane instead of the JFrame itself. The content pane will always be non-null. Attempting to set it to null will cause the JFrame to throw an exception. The default content pane will have a BorderLayout manager set on it.

Joyous answered 26/6, 2011 at 22:3 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.