The moose likes Swing / AWT / SWT and the fly likes The purpose of using getContentPane() Big Moose Saloon
  Search | Java FAQ | Recent Topics
Register / Login


Win a copy of The Mikado Method this week in the Agile and other Processes forum!
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Reply Bookmark "The purpose of using getContentPane()" Watch "The purpose of using getContentPane()" New topic
Author

The purpose of using getContentPane()

Griff Haldeman
Greenhorn

Joined: Jul 01, 2011
Posts: 5
What's the point of calling frame.getContentPane().add(button) when frame.add(button) works all the same? To my understanding, adding directly to the frame without first getting the content pane is disparaged, but if someone could elaborate on this, it'd be great.
Greg Charles
Bartender

Joined: Oct 01, 2001
Posts: 2542
    
  10

It's been awhile since I used Swing, but if I remember right, JFrame has an addImpl() method that adds components to the content pane by default. That's why frame.getContentPane.add(...) does the same thing as frame.add(...). As to why or if that's a bad practice, I'm not sure. Probably because you're not being explicit about what you're doing. There's a glass pane, where you can draw things that appear superimposed over what's in the content pane. Also a root pane and layered pane, but I forget what those do.
Griff Haldeman
Greenhorn

Joined: Jul 01, 2011
Posts: 5
Thanks, just what I wanted to know.
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19216

In Java 1.4 and before, adding components to a JFrame directly, or setting its layout directly, would result in an error (of java.lang.Error or some sub class, can't remember which one it was). You really needed to use the content pane. In Java 5.0 they decided it made more sense to just redirect those calls to the content pane.


SCJP 1.4 - SCJP 6 - SCWCD 5
How To Ask Questions How To Answer Questions
 
I agree. Here's the link: http://zeroturnaround.com/jrebel - it saves me about five hours per week
 
subject: The purpose of using getContentPane()
 
Similar Threads
Can you guess what this is?
redirect, foreward
How to make circle?
WA #1.....word association
get value from radio button