• 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:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

The purpose of using getContentPane()

 
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Sheriff
Posts: 3063
12
Mac IntelliJ IDE Python VI Editor Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks, just what I wanted to know.
 
Sheriff
Posts: 22783
131
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
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.
 
If somebody says you look familiar, tell them you are in porn. Or in these tiny ads:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic