Main Page

Previous Next

Components and Containers

A component represents a graphical entity of one kind or another that can be displayed on the screen. A component is any object of a class that is a subclass of Component. As we have seen, a JFrame window is a component, but there are many others. Before getting into specifics, let's first get a feel for the general relationship between the groups of classes that represent components. Part of the class hierarchy with Component as a base is shown overleaf. The arrows in the diagram point towards the superclass.

Click To expand

This shows some of the subclasses of Component – the ones that are important to us at the moment. We discussed the chain through to JFrame earlier, but the other branches are new. The classes that we will be using directly are all the most commonly derived classes.

Let's summarize how you would typically use the key classes in this hierarchy:

Class

Use

JFrame

This is used as the basic Java application window. An object of this class has a title bar and provision for adding a menu. You can also add other components to it. You will usually subclass this class to create a window class specific to your application. It is then possible to add GUI components or draw in this window if required, as we will see.

JDialog

You use this class to define a dialog window that is used for entering data into a program in various ways. You usually code the creation of a dialog in response to some menu item being selected.

JApplet

This is the base class for a Java 2 applet – a program designed to run embedded in a web page. All your Java 2 applets will have this class as a base. You can draw in a JApplet and also add menus and other components.

JComponent

The subclasses of JComponent define a range of standard components such as menus, buttons, checkboxes, and so on. You will use these classes to create the GUI for your application or applet.

All the classes derived from Container can contain other objects of any of the classes derived from Component, and are referred to generically as containers. Since the Container class is a sub class of the Component class, every container object is a Component too, so a container can contain other containers. The exception is the Window class and its subclasses, as objects of type Window (or of a subclass type) can't be contained in another container. If you try to do this, an exception will be thrown. The JComponent class is the base for all the Swing components used in a window as part of the GUI, so, since this class is derived from Container, all of the Swing components are also containers.

As you can see, the JApplet class, which is a base class for all Swing applets, is derived from Component via the Container class. An applet will, therefore, also inherit the methods from the Container and Component classes. It also inherits methods from the old Applet class, which it extends and improves upon. You should note that the JApplet, JFrame, and JDialog classes, and the JComponent class and its subclasses, are all in the package javax.swing. The Applet class is in java.applet, and all the others are in java.awt. The package java.applet is tiny – it only contains the one class plus three related interfaces, but we won't need to use it directly. We will always be using the JApplet class to define an applet, as it's significantly better than Applet.

Window and Frame Components

The basic difference between a JFrame object and a Window object is that a JFrame object represents the main window for an application, whereas a Window object does not – you always need a JFrame object before you can create a Window object.

Since the JDialog class is derived directly from the Window class, you can only create a JDialog object in an application in the context of a JFrame object. Apart from the default constructor, the constructors for the JDialog class generally require a JFrame object to be passed as an argument. This JFrame object is referred to as the parent of the JDialog object. A JFrame object has a border, is resizable, and has the ability to hold a built-in menu bar. Since a JFrame object is the top-level window in an application, its size and location are defined relative to the screen. A JDialog object with a JFrame object as a parent will be located relative to its parent.

Note that while we will discuss applets based on the JApplet class in this book, there is still a significant role for applets based on the more restricted capabilities of the Applet class. This is because as yet browsers do not support Java 2 applets by default. Both Netscape Navigator and Microsoft Internet Explorer require the Java Plug-In from Sun to be installed before a Java2 applet can be executed. You can download the Java Plug-In from the Sun Java web site at http://java.sun.com/products.

As we said, the JApplet, JFrame, and JDialog classes are all containers because they have Container as a base class and therefore, in principle, can contain any kind of component. They are also all components themselves since they are derived ultimately from the Component class. However, things are not quite as simple as that. You don't add the components for your application or applet GUI directly to the JFrame or JApplet object for your program. Let's look at how it actually works in practice.

Window Panes

When you want to add GUI components or draw in a window displayed from a JFrame object, you add the components to, or draw on, a window pane that is managed by the JFrame object. The same goes for an applet. Broadly speaking, window panes are container objects that represent an area of a window, and they come in several different types.

You will use a window pane called the content pane most of the time, but there are others. The relationship between the contentPane object, other window panes, and the application window itself is shown here.

Click To expand

As you see, the area below the title bar in a JFrame window corresponds to a JRootPane object. This contains another pane, the layeredPane object in the illustration, which is of type JLayeredPane. This pane corresponds to the whole of the area occupied by the JRootPane object in the window and manages the menu bar if the window has one. The area in the layeredPane below the menu bar corresponds to the contentPane object, and it's here that you typically add GUI components. You also display text or do any drawing in the area covered by the content pane.

The layeredPane object has special properties for advanced applications that permit groups of components to be managed in separate layers that overlay one another within the pane. With this capability you can control how components are displayed relative to one another, because the layers are displayed in a particular order from back to front. The components in a layer at the front will appear on the screen in front of those in a layer that is towards the back.

There is also an additional pane not shown in the diagram. This is the glassPane object, and this also corresponds to the complete JRootPane area. The contents of the glassPane object displays on top of all the other panes, so this is used to display components that you always want to display on top of anything else displayed in the window – such as pop-up menus. You can also use the glassPane to display graphics that need to be updated relatively frequently – such as when you create an animation. When part of what is displayed is to be animated, a static background can be displayed independently via the contentPane. Since this doesn't need to be reprocessed each time the animated objects need to be redrawn, the whole process can be much more efficient.

The JFrame class defines methods to provide you with a reference to any of the panes:

Method

Description

getRootPane()

Returns the root pane as type JRootPane.

getLayeredPane()

Returns the layered pane as type JLayeredPane.

getContentPane()

Returns the content pane as type Container. This is the method you will use most frequently, since you normally add components to the content pane.

getGlassPane()

Returns the glass pane as type Component.

All the classes discussed here that represent panes are themselves Swing components, defined in the javax.swing package. A JApplet object has the same arrangement of panes as a JFrame object, so adding components to an applet, or drawing on it, works in exactly the same way. An applet defined as a JApplet object can also have a menu bar just like an application window.

All the panes, as well as the menu bar, are components, so before we start delving into how to add a menu bar or other components to a window, let's unearth a little more about the make-up of components in general.

Previous Next
JavaScript Editor Java Tutorials Free JavaScript Editor


North Richland Hills bankruptcy lawyers . nda form