Results 1 to 2 of 2
- 03-22-2014, 11:45 AM #1Member
- Join Date
- Feb 2014
- Rep Power
Struggling to understand content panes
Im reading up on the swing package and am struggling to understand content panes.
This is my understanding so far. Firstly we need a top level container such as a JFrame. This container will then require a content pane? I assume a content pane is simply where everything else is contained or what exactly does a content pane consist of/do?
Now reading from the tutorial it says:
"Adding Components to the Content Pane
Here's the code that the preceding example uses to get a frame's content pane and add the yellow label to it:
As the code shows, you find the content pane of a top-level container by calling the getContentPane method. The default content pane is a simple intermediate container that inherits from JComponent, and that uses a BorderLayout as its layout manager.
It's easy to customize the content pane — setting the layout manager or adding a border, for example. However, there is one tiny gotcha. The getContentPane method returns a Container object, not a JComponent object. This means that if you want to take advantage of the content pane's JComponent features, you need to either typecast the return value or create your own component to be the content pane. Our examples generally take the second approach, since it's a little cleaner. Another approach we sometimes take is to simply add a customized component to the content pane, covering the content pane completely.
Note that the default layout manager for JPanel is FlowLayout; you'll probably want to change it.
To make a component the content pane, use the top-level container's setContentPane method. For example:
//Create a panel and add components to it.
JPanel contentPane = new JPanel(new BorderLayout());
So unless I specify a new content pane it just uses the default which is fair enough but if you want to change specific things in the content pane you should create your own. My question is are all newly created content panes going to be a JPanel? if not how do I know what type to use for a content pane?
Sorry if these questions are silly but I am struggling and would really appreciate some help.
- 03-22-2014, 04:55 PM #2Senior Member
- Join Date
- Jan 2013
- Northern Virginia, United States
- Rep Power
Re: Struggling to understand content panes
It is real simple. The content pane is already set for you. I rarely change it. If you want to see what it is you can do a getContentPane() and examine its type. And you don't have to retrieve the contentPane to add stuff to it. You used to before Swing, but the Frame.add() adds components to the content pane for you.
I tend to think of the content pane as just part of the JFrame. Which means I will usually add my own JPanel to it to hold other components or do graphics.
You should also read about the glassPane and rootPane. How to Use Root Panes (The Java™ Tutorials > Creating a GUI With JFC/Swing > Using Swing Components)
- By njliven in forum New To JavaReplies: 1Last Post: 10-24-2012, 11:48 PM
- By Nidi in forum New To JavaReplies: 2Last Post: 03-02-2011, 04:27 AM
- By teckno101 in forum AWT / SwingReplies: 1Last Post: 10-03-2009, 11:16 PM
- By skatefreak in forum New To JavaReplies: 1Last Post: 08-09-2009, 10:21 AM
- By Leprechaun in forum New To JavaReplies: 1Last Post: 01-31-2008, 04:02 AM