Question

I am learning Swings and i am confused with this one line

GroupLayout layout=new GroupLayout(getContentPane());

now i have 2 questions

  1. what does getContentPane() return . [i saw the docs and got more confused ]
  2. why are we passing it to GroupLayout , i mean how getContentPane() is used to Group Layout
Was it helpful?

Solution

What does getContentPane() return

It returns component's content pane

  • To appear onscreen, every GUI component must be part of a containment hierarchy. A containment hierarchy is a tree of components that has a top-level container as its root.
  • Each GUI component can be contained only once. If a component is already in a container and you try to add it to another container, the component will be removed from the first container and then added to the second.
  • Each top-level container has a content pane that, generally speaking, contains (directly or indirectly) the visible components in that top-level container's GUI.
  • You can optionally add a menu bar to a top-level container. The menu bar is by convention positioned within the top-level container, but outside the content pane. Some look and feels, such as the Mac OS look and feel, give you the option of placing the menu bar in another place more appropriate for the look and feel, such as at the top of the screen.

You can read about it more here

Why are we passing it to GroupLayout , i mean how getContentPane() is used to Group Layout

That's how GroupLayout is implemented.

Constructor:

GroupLayout(Container host)

Creates a GroupLayout for the specified Container. Please refere to javadoc for more

OTHER TIPS

  1. what does getContentPane() return . [i saw the docs and got more confused ]

    getContentPane() function of JFrame returns Container object to which you can add other components you wish on the JFrame.

  2. why are we passing it to GroupLayout , i mean how getContentPane() is used to Group Layout

    GroupLayout layout=new GroupLayout(getContentPane());

Function is

/**
 * Creates a {@code GroupLayout} for the specified {@code Container}.
 *
 * @param host the {@code Container} the {@code GroupLayout} is
 *        the {@code LayoutManager} for
 * @throws IllegalArgumentException if host is {@code null}
 */
public GroupLayout(Container host) {
    if (host == null) {
        throw new IllegalArgumentException("Container must be non-null");
    }
    honorsVisibility = true;
    this.host = host;
    setHorizontalGroup(createParallelGroup(Alignment.LEADING, true));
    setVerticalGroup(createParallelGroup(Alignment.LEADING, true));
    componentInfos = new HashMap<Component,ComponentInfo>();
    tmpParallelSet = new HashSet<Spring>();
}

This constructor statements creates GroupLayout for the specifies container.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top