Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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.

like image 897
froehli Avatar asked Jun 26 '11 21:06

froehli


People also ask

What is frame getContentPane ()?

The getContentPane() method retrieves the content pane layer so that you can add an object to it. The content pane is an object created by the Java run time environment.

What does setContentPane do in Java?

A JPanel is a Container , JFrame uses setContentPane , which allows us to set a container, thus JPanel / JComponent extends Conatiner so that it can be added as a contentPane.

What is setDefaultCloseOperation in Java?

The default behavior is to simply hide the JFrame when the user closes the window. To change the default behavior, you invoke the method setDefaultCloseOperation(int) . To make the JFrame behave the same as a Frame instance, use setDefaultCloseOperation(WindowConstants. DO_NOTHING_ON_CLOSE) .

What is content pane in JFrame?

JFrames have a content pane, which holds the components. These components are sized and positioned by the layout manager when JFrame's pack() is called. Content pane border. There are several ways to handle the content pane, but most of them fail to provide one basic requirement -- ability to set a border.


3 Answers

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.

like image 196
Hovercraft Full Of Eels Avatar answered Oct 27 '22 17:10

Hovercraft Full Of Eels


//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.

like image 43
camickr Avatar answered Oct 27 '22 19:10

camickr


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.)

like image 39
Alan Avatar answered Oct 27 '22 17:10

Alan