Win a copy of Beginning Java 17 Fundamentals: Object-Oriented Programming in Java 17 this week in the Java in General forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Tim Cooke
  • Campbell Ritchie
  • Ron McLeod
  • Liutauras Vilda
  • Jeanne Boyarsky
Sheriffs:
  • Junilu Lacar
  • Rob Spoor
  • Paul Clapham
Saloon Keepers:
  • Tim Holloway
  • Tim Moores
  • Jesse Silverman
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • Al Hobbs
  • Piet Souris
  • Frits Walraven

GetContentPane()

 
Antonio Moretti
Ranch Hand
Posts: 77
2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I'm having some trouble understanding this method. Some tutorials use it to add components to JFrame, and some don't. Most of what I could find online is just regurgitated explanation that did not clarify. I would like to know what it is for and when we need to use it. Thanks!
 
Campbell Ritchie
Marshal
Posts: 74722
336
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Antonio Moretti wrote:. . . Some tutorials use it to add components to JFrame, and some don't. . . . Thanks!

Check the age of the tutorials; very old code did use getContentPane(), but it is rarely used nowadays. If you look up Container#add(), you will find that method delegates to this method. Both by default add the new Component where you instruct it, in most cases on the content pane automatically. So there is usually no need to call getContentPane() at all. It is possible to reassign the content pane, but I have never felt the need even to try that.
Look through the Java™ Tutorials; you will find that of the four kinds of top level container (dialogue, applet, window, and frame), some have as it were four layers on top of them. The link at the bottom of the tutorial about root panes explains a bit more.
That tutorial link tells you you don't need to call getContentPane():-

That Tutorial page wrote:As a convenience, the add method and its variants, remove and setLayout have been overridden to forward to the contentPane as necessary.



Note: nobody uses applets any more. Windows don't have menu bars or close buttons, so they are very rarely used; most people only use frames and dialogues. JFrame is a kind of frame.
 
Stephan van Hulst
Saloon Keeper
Posts: 13500
305
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You rarely need to call getContentPane(). Most of the methods that you'd normally want to call on a frame's content pane have a twin method that you can instead call on the frame directly.

For instance, JFrame.add(Component) does the same thing as JFrame.getContentPane().add(Component), so there is no reason to call getContentPane() when you just want to add a component to it.

I haven't worked with Swing in a while, but I seem to recall the only time I actually call getContentPane() is when I want to use a BoxLayout for the content pane. BoxLayout requires you to pass the container into its constructor, so you need a direct reference to the content pane.
 
Antonio Moretti
Ranch Hand
Posts: 77
2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Campbell Ritchie wrote:. . . . Check the age of the tutorials . . . JFrame is a kind of frame.



Thanks. It's kind of confusing because Swing is making a come back and I guess there's a lot of old stuff out there. I'm glad I don't have to bother with this method.
 
Rob Spoor
Sheriff
Posts: 22574
122
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Stephan van Hulst wrote:You rarely need to call getContentPane(). Most of the methods that you'd normally want to call on a frame's content pane have a twin method that you can instead call on the frame directly.

For instance, JFrame.add(Component) does the same thing as JFrame.getContentPane().add(Component), so there is no reason to call getContentPane() when you just want to add a component to it.


That was only added in Java 5*. Before that time, you had to use getContentPane(), and any methods related to the contents (including setLayout as well) threw some type of exception.

* You can download the Java 1.4.2 API, but you need an account. It's available online at https://javaalmanac.io/jdk/1.4/api/index.html. The Java 5 API is available at https://docs.oracle.com/javase/1.5.0/docs/api/. And yes, that's how terrible the Java API used to look...
 
Campbell Ritchie
Marshal
Posts: 74722
336
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Java5? I thought that was when the convenience methods were added, but I can't seem to find the reference to Java5. Can you remember where it is?
And Java5 came out 17 years ago; I think it was regarded as obsolete 7½ years ago.
 
Paul Clapham
Sheriff
Posts: 26963
84
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The add(Component) method of JFrame(Component) is inherited from the add(Component) method of Container. The latter, according to the documentation, is a convenience method for addImpl(Component, Object, int).

And that method is a protected method in JFrame, so that's what the JFrame.add(Component) method calls eventually.

Also from the documentation, all of the methods mentioned above are from Java 1.0 except for Container.addImpl(Component, Object, int) which is from Java 1.1. Which is very odd, since JFrame.addImpl(Component, Obj, int) is from Java 1.0 -- at least the docs don't say otherwise.

But I've been writing Swing since Java 4 and I'm sure that having to use the getContentPane method was still an annoyance then. So without following Rob's links I would believe that he's correct and that some history has been lost in the process of updating the documentation.



 
Campbell Ritchie
Marshal
Posts: 74722
336
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Paul Clapham wrote:. . . I would believe that [Rob]'s correct and that some history has been lost . . .

\i am 99% sure Rob is right, too bui not 100% because I couldn't find a since number. It had never occurred to me that historical information might have been lost.
 
Rob Spoor
Sheriff
Posts: 22574
122
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I am right ( ), but that's because I've compared the documentations of Java 1.4.2 and 5 before posting. I first thought that the convenience methods were already working in Java 1.4.2 but the latter still mentioned having to use getContentPane(), and addImpl still said it threw an exception.

These convenience methods were always present, they just didn't work before Java 5. I guess enough people complained that these methods threw exceptions that Sun made these methods delegate instead of throw.
 
Rob Spoor
Sheriff
Posts: 22574
122
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Another thing about @since - it wasn't always added as correctly as it is now. I've seen discrepancies between when methods starting showing up in the API docs and what their @since reported when working on my "what's new" pages. These days Oracle's change procedure is a lot stricter about it (although it's probably still possible that someone forgets it).
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic