0

The code is:

JFrame jframe = new JFrame("no difference");
JButton button = new JButton("no difference");
jframe.getContentPane().add(button); 
// the same result occurs for
jframe.add(button);

getContentPane returns a Container object. What is the point of adding button to the Container if it works equally well with the JFrame? Why bother coding jframe.getContentPane().add(button); if we can do jframe.add(button);

mKorbel
  • 109,525
  • 20
  • 134
  • 319
Aleksei Nikolaevich
  • 325
  • 3
  • 15
  • 40
  • 1
    You should consider reading http://stackoverflow.com/questions/2432839/what-is-the-relation-between-contentpane-and-jpanel – Jeremy D Oct 03 '13 at 19:21
  • The frame's [root pane](http://docs.oracle.com/javase/tutorial/uiswing/components/toplevel.html#rootpane) _is_ a [`Container`](http://docs.oracle.com/javase/7/docs/api/javax/swing/JRootPane.html). – trashgod Oct 03 '13 at 19:25
  • 1
    jframe.add(button) did not used to be equivalent, for what reason I never did know. But the JFrame has several associated containers, and the ContentPane is the one where one would (normally) want a button to go. So you need to put it in the content pane, you used to have to do it explicitly, and now they've made it more convenient with the add method on JFrame itself. – arcy Oct 03 '13 at 19:29

3 Answers3

3

From JFrame javadoc

As a conveniance add and its variants, remove and setLayout have been overridden to forward to the contentPane as necessary.

This means there's no point in adding components through JFrame.getContentPane().add(), because JFrame.add() will do so.

I would suggest you take a look to this article which explain content pane and how to deal with it: ContentPane (or Content Pain?)

dic19
  • 17,821
  • 6
  • 40
  • 69
2

What is the difference between adding an element to a JFrame as opposed to a Container?

The other answers have covered the actual core of the question, as to why the effect is the same in the case you state. So I'll add another difference. Versatility.

The JFrame (being a top-level container), cannot be added to anything else, while the latter (being a simple container) can be added to the following:

  • JFrame
  • JApplet
  • JWindow
  • JDialog
  • JScrollPane
  • A tab of a JTabbedPane
  • One area of a JSplitPane
  • A single area of a layout in another panel.
  • ...
Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
  • @vopros voprosovich: Use [`CTRL+SHIFT+F1`](http://stackoverflow.com/q/6671021/230513) to see a listing of the _containment hierarchy_. – trashgod Oct 04 '13 at 16:00
  • @trashgod DYM to use those short-cuts in the Swing Explorer linked from that thread? Oh.. I see now that is not what you meant.. :P – Andrew Thompson Oct 04 '13 at 16:12
0

Since javax.swing.JFrame is a child class of java.awt.Container, so jframe.add(button) actually calls add() from the Container. Thats why its same.

  • 2
    No, it does not use `add` from the base class. It **delegates** `add` to its only child component, which is a `JRootPane`. Earlier, this delegation had to be done manually by using `getContentPane()`, but since 1.5 this delegation is done automatically. See http://docs.oracle.com/javase/7/docs/api/javax/swing/JFrame.html – Andreas Fester Oct 03 '13 at 19:36