Brought to you by EarthWeb
ITKnowledge Logo Login Graphic Click Here!
Click Here!
ITKnowledge
Find:
 
EXPERT SEARCH ----- nav

EarthWeb Direct

EarthWeb Direct

EarthWeb sites: other sites

Previous Table of Contents Next


Part II
JFC Components

In This Part
•  Basic Componens
•  The Button Hierarchy
•  Text Components
•  Frame Windows
•  Menus and Toolbars
•  JList, JComboBox, and Bound Controls
•  Tree Component
•  Table Componenty

Chapter 5
Basic Components

In This Chapter
•  Separator
•  Borders
•  Icons
•  Labeled Components

When building a user interface, there is a need for basic user interface components. This chapter presents the basic components supplied with the JFC.

In this chapter you will learn how to use:

  Separators
  Borders
  Icons
  Labels

Separator

Perhaps the simplest component in the JFC is the JSeparator class. The JSeparator displays an etched line that is typically used to divide areas in a panel (see Figure 5.1). This class is commonly used in menus but can be used in any container.


Figure 5.1  A JSeparator.

Creating a separator is a simple task. Simply call the constructor of the class—it doesn’t take any parameters. This is shown in the following code fragment. The separator can then be added to a container. Many of the examples in the remainder of this book will show the JSeparator class in action.

JSeparator separator = new JSeparator();

Borders

Borders are often used to group components that perform a related function. The JFC makes this particularly easy because the JComponent class provides a property for a border. This means that any JComponent, both simple components and complex containers, can have a border. Also, borders can be nested to create any visual appearance desired.

The Border Interface

The methods that define a JFC border are contained in the Border interface. These methods are shown in the next code example. Any component can implement this interface and be used as a border for a JComponent. However, as you will see in the remainder of this section, the JFC provides a rich set of standard borders. Due to the stock borders, implementing the Border interface is rarely required.

public interface Border
{
    /**
     * Paints the border for the specified component with the specified
     * position and size.
     * @param c the component for which this border is being painted
     * @param g the paint graphics
     * @param x the x position of the painted border
     * @param y the y position of the painted border
     * @param width the width of the painted border
     * @param height the height of the painted border
     */
void paintBorder( Component c, Graphics g,
                      int x, int y, int width, int height );

    /**
     * Returns the insets of the border.
     * @param c the component for which this border insets value applies
     */
    Insets getBorderInsets(Component c);

    /**
     * Returns whether or not the border is opaque.  If the border
     * is opaque, it is responsible for filling in it’s own
     * background when painting.
     */
    boolean isBorderOpaque();
}

The Border Package

The borders that are part of the JFC are contained in the javax.swing.border package. This package consists of the Border interface itself, an abstract border class that serves as the parent to the eight concrete borders, and the concrete border classes. The border class hierarchy is shown in Figure 5.2.


Figure 5.2  The Border package class hierarchy.

An instance of a concrete border can be created and set for any JComponent. The following code shows how to create a SoftBevelBorder instance. The remaining borders will be presented in the BorderFactory section later in this chapter. The next section shows how to add the border to a JComponent.

Border border = new SoftBevelBorder( BevelBorder.LOWERED );

Adding a Border to any JComponent

The setBorder method in JComponent is provided for setting the border. Because this method is in JComponent, any JFC component, or any component derived from a JFC component, can contain a border. This means that both simple components, such as JLabel instances, and container components, such as JPanel instances, can contain a border. This makes it extremely easy to add borders to group-related components in a panel. This is a powerful feature of the JFC. An example of the setBorder method will be shown in the next section.

The getBorder method can be used to query the current border surrounding a JComponent. The returned border can then be altered for the current situation. This method will return null if the component doesn’t currently have a border.

The BorderFactory

Instances of the borders in the Border package can be instantiated directly, as shown in a previous section. However, the JFC borders are designed to be shared among components. To facilitate sharing borders, a factory has been built to retrieve borders. The BorderFactory consists of a collection of static methods used to obtain a handle to a border. The BorderFactor lives in the javax.swing package, even though the borders themselves live in the javax.swing.border package. These methods follow the naming pattern createTypeBorder, where Type is one of the border classes in the javax.swing.border package. There is a create method that corresponds to each public constructor in the border classes. Each of the borders that can be retrieved via the BorderFactory is discussed in the following sections.


Previous Table of Contents Next
HomeAbout UsSearchSubscribeAdvertising InfoContact UsFAQs
Use of this site is subject to certain Terms & Conditions.
Copyright (c) 1996-1999 EarthWeb Inc. All rights reserved. Reproduction in whole or in part in any form or medium without express written permission of EarthWeb is prohibited. Read EarthWeb's privacy statement.