Results 1 to 2 of 2
  1. #1
    Dark Knight is offline Member
    Join Date
    Feb 2014
    Posts
    20
    Rep Power
    0

    Default Struggling to understand content panes

    Hi,

    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:

    frame.getContentPane().add(yellowLabel, BorderLayout.CENTER);
    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());
    contentPane.setBorder(someBorder);
    contentPane.add(someComponent, BorderLayout.CENTER);
    contentPane.add(anotherComponent, BorderLayout.PAGE_END);

    topLevelContainer.setContentPane(contentPane);"

    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.

    Thanks.

  2. #2
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    3,781
    Rep Power
    5

    Default 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)

    Regards,
    Jim
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

Similar Threads

  1. Tabbed Panes
    By njliven in forum New To Java
    Replies: 1
    Last Post: 10-24-2012, 11:48 PM
  2. JTab Panes
    By Nidi in forum New To Java
    Replies: 2
    Last Post: 03-02-2011, 04:27 AM
  3. Layered Panes
    By teckno101 in forum AWT / Swing
    Replies: 1
    Last Post: 10-03-2009, 11:16 PM
  4. panes, frames and ddm's :|
    By skatefreak in forum New To Java
    Replies: 1
    Last Post: 08-09-2009, 10:21 AM
  5. Two content panes in one GUI?
    By Leprechaun in forum New To Java
    Replies: 1
    Last Post: 01-31-2008, 04:02 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •