New to Java
Programming Center
Java Platform Overview |
Getting Started |
Step-by-Step Programming
Learning Paths |
References & Resources |
Certification |
Supplements

Building an Application:
Part 2: Introduction to Inheritance, Panels, and Layouts
by Dana Nourie
December 2001
Contents
BACK<<Inheritance | Containers and Components |
NEXT>>Layout Managers
Which class should the Welcome class extend?
The Welcome class extends the JPanel class, making it a JPanel object without having to instantiate the JPanel class with the
new keyword. In addition,
the JPanel is an important type of container.
Containers and Components
Containers and components are the ingredients of the GUI construction kit on the
Java platform. The java.awt and
javax.swing packages provide predefined containers and components
to use in your applications, and
you can develop or customize your own.
The Dive Log application uses predefined containers and components,
customized to meet the needs of a simple dive log. In the DiveLog class, you created a frame
container, either by extending to the JFrame class, or by creating a JFrame object
with the keyword new. In addition, you created a second container: JTabbedPane, on
which you initialize containers and components.
Before continuing with the Welcome class, where you add another container and components,
you should understand the purpose of containers and components.
Containers
Real world objects need containers. Your car has a frame that holds other types of containers such as doors, a trunk, a front-end that encloses the engine, and a compartment that contains passenger objects like seats and controls to drive the car. In addition, your car has numerous other containers with smaller parts, or components, within.
Software application containers also hold containers that hold numerous objects and often other containers.
A GUI application has at least these objects:
- A frame container to hold the visual and non-visual parts of the GUI.
- A panel container that is sectioned into a specific layout.
- Components that are added to the panel within the specific layout sections.
- Open the
Welcome.java file in your text editor.
- Extend the
Welcome class to JPanel in your
class declaration if you have not done so already in Part 1:
public class Welcome extends JPanel
{ //Opens class
}//Closes class Welcome
- Save the file.
|
Since the Welcome class extends the JPanel class, you are making this object a panel, in particular a JPanel, which is a container. Now you've created an application with:
- A frame container that contains a
- content pane container that contains a
- tabbed pane container that contains a
- panel container to hold other containers or components

Containers created so far
|
Components
Software components enable users to interact with an application, both passively and actively:
- Passively
Components display text, titles, and images.
- Actively
Components enable users to request or give information through buttons, menus,
radio buttons, text fields (holds short strings), and text areas (holds long strings).
You see many components on the browser you are using now, such as:
- Buttons
- Scroll bars
- Text fields
- Text and images
The Project Swing library has many predefined components
available for use in your applications. When you create components, these
objects can inherit methods from the JComponent class, such as:
setBackground(Color bg)
setForeground(Color fg)
setOpaque(boolean isOpaque)
setToolTipText(String text)
setVisible(boolean aFlag)
Some of these methods should look familiar, as they were used in the DiveLog class.
The important point about these methods is that you can call them on any component
you create. You'll see those methods at work soon.
In the next programming exercise, you will create these passive components, shown below:
- Two images
- Text
- A text area
- Border with a title

Components in the Welcome class
|
And these objects:
- Label with an image and text
- Text area
- Second label with an image
- Open the
Welcome.java file in your text editor.
- Add the following variables just after the opening curly brace for the class. This variables do not yet have assignments:
-
private JLabel jl;
-
private JTextArea ta;
-
private JLabel diver;
- Save the file.
|
These variables are declared private. There isn't any reason for outside classes
to access them directly. In addition, these variables aren't given their assignments yet, because declaring them outside a method makes them available to the rest of the class. Initializing the
variables within the class constructor forces these objects to be built when the class itself is initialized.
Setting up the rest of the Welcome panel takes four steps. Here, step two is missing:
- Declare variables.
- Create GUI objects, such as labels, images, and so forth.
- Add the objects to the panel.
|