Page 1 of 1

Swing Component Overview: Top-Level Components JFrame, JApplet, and JDialog Rate Topic: -----

#1 Dogstopper  Icon User is offline

  • The Ninjaducky
  • member icon



Reputation: 2876
  • View blog
  • Posts: 11,051
  • Joined: 15-July 08

Posted 24 April 2010 - 09:16 PM

Java Swing Overview– Top-Level Components
Top Level Components in Swing are some of the generally most misunderstood components in Swing, but they sit at the root of all Swing applications. Therefore, it is important to understand how to use them and what they are. There are three Top Level Components – JFrame, JApplet, and JDialog. What make these components important is the fact that they are the only components that do not subclass JComponent as the rest of the Swing library does. There are actually 6 pieces that make up each one of these top-level components, and those pieces are what make using them difficult for certain tasks.

Each top-level component has a Frame (or Applet or Dialog) at the base with the Root Pane sitting on top of it. On top of the Root Pane is theLayeredPane, on which the Content Pane with a Menu Bar sit. Finally, on top, there is a GlassPane. Now, knowing what these do is really important to the functions of the top-level component. Let's begin with the Root Pane.

Root Pane
I'm going to give you “The Gist”. Basically, what a Root Pane does is manage all the other components that are on top of it, including the Content Pane, the Menu Bar, and all the others I mentioned above. It controls what goes on inside the JFrame, and for the most part, you don't have to worry about it. However, if you ever want to intercept events or paint on top of several components, you may have to learn how to use a Root Pane.

Layered Pane
This pane is tasked with maniulating the Z-axis on components inside of it. You can directly add a component to the LayeredPane and add a component and specify a depth for it (the bigger the number, the deeper it is - where 0 is the top and negative numbers are last.) You can achieve this simply by making a new JLayeredPane and adding it to a JPanel. Since this tutorial is about Top Level components, I won't talk much more about it. I will (and if you are reading this after the fact have) write a tutorial about them later.

Content Pane
This is where most of the visible action occurs and where all the components are added and layouts managed and backgrounds set...However, three methods are automatically forwarded to the JContentPane: add(), remove(), and setLayout(). To add any component to a JFrame, you should do this:
jFrame.add(component);



However, it is legal to add it by first getting the content pane.
jFrame.getContentPane().add(component);



And that is the way that you perform any function that is not add(), remove(), or setLayout().

So, to set a background on a JFrame would look like this:
public class Background extends JFrame {
    public Background() {
        this.getContentPane().setBackground(Color.RED);
    }
}



Now, most new students would just try to use setBackground() instead of getting the contentPane, but remember the only three methods that are redirected for you. The rest must utilize this wonderful conentPane.

Glass Pane
The Glass Pane is the method by which the root pane can interfere with events and can paint over top of any components, because it IS on top. By default, it is hidden, but when needed, it is on top of the contentPane and can do things like paint. I will definitely make a tutorial on this later.

Uses of Top Level Components
Use a Top Level Component when you want to:
  • Make an application and need a root to begin with.
  • Add JPanels and Other Containers.
  • Need to deal with WindowEvents.


DON'T use a Top Level Component when you want to:
  • Add tons of non-container components (You should instead add them to JPanels or other Containers and then add the groups to the top-level.
  • Do Graphics. (Since you have to use the content pane, this can be difficult. Also, it's much easier to subclass a JPanel anyway).
  • Have large amounts of event-listeners


So basically, a top-level component should be a gateway to the rest of the program, but most of your functional code should exist in components like buttons, panels, and other components. It is much better to make each component do what it was tasked to do. A top-level component should be the foundation onto which you build, but most of the functionality should occur in children components.

Conclusion
I hope that I have given you an adequate overview on what top-level components are and their uses and misuses. This is the Overview of all Swing parts and is by far the most difficult to explain, but I hope you realize its significance.

Is This A Good Question/Topic? 2
  • +

Replies To: Swing Component Overview: Top-Level Components

#2 Luckless  Icon User is offline

  • </luck>
  • member icon

Reputation: 293
  • View blog
  • Posts: 1,146
  • Joined: 31-August 09

Posted 30 April 2010 - 04:21 AM

great tutorial, as usual. This is really excellent. Can't wait for the root pane/ glass pane tutorial
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1