2

I am new to programming and was just studying JFrame etc , here i read ContentPane Like this "Components go in the "content pane", not directly in the frame." when i did search COntentpane i read containment hierarch that is pretty confusing for me to understand ,

Can some one please clear me the concept of ContentPane and containment hierarchy of Gui Component

  • 3
    It's all explained in [the tutorial](http://docs.oracle.com/javase/tutorial/uiswing/components/toplevel.html). What don't you understand? – JB Nizet Nov 25 '12 at 17:12
  • for example we are using JFrame f= new JFrame(); and then Container content = f.getContentPane(); content.add(new JButton("Button 1")); and same way it goes , why not Directly frame.add(button) and so on .. why we add content pane here –  Nov 25 '12 at 17:15
  • 4
    Because that's what you're supposed to do, because it has been designed like that and documented. Just like to go faster with your car, you must press the right pedal and not the left one. You can add the components to the frame directly, though, because adding it to the frame actually adds it to the content pane for you. – JB Nizet Nov 25 '12 at 17:21
  • Ok , that clears the idea Thanks for help . –  Nov 25 '12 at 17:33

1 Answers1

7

I think this diagram describes distinctly what you want:

enter image description here

Maxim Shoustin
  • 77,483
  • 27
  • 203
  • 225
  • for example we are using JFrame f= new JFrame(); and then Container content = f.getContentPane(); content.add(new JButton("Button 1")); and same way it goes , why not Directly frame.add(button) and so on .. why we add contentpane here while it can be dont without contentpane –  Nov 25 '12 at 17:18
  • 1
    @SikanderNawaz In newer Java version as a convenience, the `add` method and its variants, `remove` and `setLayout` have been overridden to forward to the contentPane as necessary. – David Kroukamp Nov 25 '12 at 19:32