In general, Swing components except the top-level containers extend the JComponent class which extends Container class. So, any swing component that extends JComponent is light-weight. The top-level containers which are discussed previously, are heavy-weight because they must depend on the OS. In this tutorial you will learn:
- General class hierarchy of light-weight components.
- What is containment hierarchy? What is a root?
- What is a content pane and a root pane?
java.lang.Object
|
+--java.awt.Component
|
+--java.awt.Container
|
+--javax.swing.JComponent
|
+--Any light-weight component
Containment Hierarchy
This might seem quite weird, but we can kill it! You already knew that a top-level container contains all the [components] or [light-weight containers (for example, JPanel) which again contain components]. This hierarchy that contains all the components is called as Containment Hierarchy. Every containment hierarchy has its root which is a top-level container. So, how many containment hierarchies can be in an application? Well, the answer is it is equal to the no.of top-level containers in the application. For example, if the application contains 1 JFrame and 4 dialogs, then it has got 5 top-level containers, so 5 containment hierarchies.
Note that containment hierarchies are different for applets and stand alone applications as their roots differ. In applets, the root is JApplet and for stand alone apps it is JFrame.
What is a content pane?
A content pane is an abstract panel on a top-level container that holds components. You already knew that a component cannot be displayed if it isn't add to a container. So, in order to add components to a container, we need to get its content pane and add to it. Programmers, previously wrote
myFrame.getContentPane().add(new JButton("Button"));
to write add a simple JButton to a JFrame however, we no longer require call to this method except in the case of setting background for JFrame.Only top-level containers in Swing has this method.
The great thing in Swing is every component is a container. Yes, because every component, including the top-level container has the add() method. Well, another reason that will strongly support this argument is JComponent is a sub-class of Container.
The Root Pane
Root Pane is an abstract layer on a top-level container. This root pane contains several panes in it like the Layered pane, content pane and menu bar and glass pane. Though, we don't use it typically, we can.
Now you might be clear about the general class hierarchy of light weight swing components and the containment hierarchy.
Now you might be clear about the general class hierarchy of light weight swing components and the containment hierarchy.
Previous: MVC Architecture in Swing
No comments:
Post a Comment