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


EmptyBorder

The static BorderFactory method createEmptyBorder returns an instance of the EtchedBorder class. As the name implies, and empty border is a border that is not painted. The first question that comes to mind is, why would such a border be desired? The answer is that empty borders have replaced Insets from the AWT as the desired mechanism for adding space around a component.

The BorderFactory contains two static overloaded methods to obtain a handle on an EmptyBorder. The more interesting of the two createEmptyBorder methods is the version that takes parameters. The amount of space taken by the border on each side of the bordered component can be specified by using this method. Notice that the space on each side is individually specified in the same order as when creating an Insets instance. This allows a non-symmetric border, if desired. A picture of an EmptyBorder is not presented, because it is difficult to see.

Border border = BorderFactory.createEmptyBorder();
Border iBorder = BorderFactory.createEmptyBorder(
                       top, left, bottom, right );

MatteBorder

The static BorderFactory method createMatteBorder returns an instance of the MatteBorder class. The method to obtain a matte border is similar to the method to obtain an empty border. This is because the matte border extends the EmptyBorder class. In addition to the border size parameters, a color or icon can be specified when creating a matte border.

When a color is specified, the border looks suspiciously like a line border. The fundamental difference between the two borders is that the line border is the same thickness on each side of the bordered component. The matte border can be a different size on each side of the component. The following line of code retrieves a blue matte border with a thickness of five pixels from the BorderFactory (see Figure 5.6).

JLabel label = new JLabel( “Matte Border” );
Border border = BorderFactory.createMatteBorder(
                      5, 5, 5, 5, Color.blue );
label.setBorder( border );


Figure 5.6  A matte border.

When given an Icon, the matte border paints the Icon in the border. The Icon is repeated as often as necessary to fill the border. The following line of code creates a matte border by using the Icon named checker. A simple red and white checked pattern icon was used to create the checkered matte border in Figure 5.7. From these figures, it can be seen how the icon image is repeated over and over to fill the border.

//
// Assume the checker.gif image is in the current directory.
//
ImageIcon checker = new ImageIcon( “checker.gif” );
JLabel label = new JLabel( “Matte Border” );
Border border = BorderFactory.createMatteBorder( 4, 4, 4, 4, checker );
label.setBorder( border );


Figure 5.7  A checkered matte border created by repeating a checker icon.

CompoundBorder

In the previous sections, you looked at the simple borders contained in the JFC. Borders can be combined to create an endless variety of compound borders. The BorderFactory static method createCompoundBorder returns an instance of a compound border. A compound border consists of two borders, one nested inside the other. A typical use of a compound border is to add space between a decorative border and the component.

In the examples given in the previous sections, you saw that the label’s text approaches the border. This is awkward in appearance. Using a compound border can alleviate this problem. An empty border with the desired margin can be placed inside the decorative border. The following line of code creates such a border. The first parameter is the outer border, and the second is the inner border (see Figure 5.8). In the figure it is easy to see how the empty border provides a margin between the label’s text and the matte border.

//
// Assume the checker.gif image is in the current
// directory.
//
ImageIcon checker = new ImageIcon( “checker.gif” );
JLabel label = new JLabel( “Matte Border” );
Border border = BorderFactory.createCompoundBorder(
            BorderFactory.createMatteBorder( 4, 4, 4, 4, checker ),
            BorderFactory.createEmptyBorder( 4, 4, 4, 4 ) );
label.setBorder( border );


Figure 5.8  A compound matte and empty border.

Compound borders can be nested to add additional visual appeal. This is accomplished by creating a CompoundBorder as shown earlier, and then using this border as the outside border in another CompoundBorder. This nesting can be performed to an arbitrary depth. The following code fragment creates a border consisting of a matte border surrounded by bevel borders. This creates a sunken matte border (see Figure 5.9).

//
// Assume the checker.gif image is in the current
// directory.
//
ImageIcon checker = new ImageIcon( “checker.gif” );
JLabel label = new JLabel( “Sunken Matte Border” );
Border border = BorderFactory.createCompoundBorder(
            BorderFactory.createBevelBorder( BevelBorder.LOWERED ),
            BorderFactory.createMatteBorder( 4, 4, 4, 4, checker ) );
border = BorderFactory.createCompoundBorder(
            border,
            BorderFactory.createBevelBorder( BevelBorder.RAISED ) );
border = BorderFactory.createCompoundBorder(
            border,
            BorderFactory.createEmptyBorder( 2, 2, 2, 2 ) );
label.setBorder( border );


Figure 5.9  Double nested CompoundBorder.

Restraint must be shown when using multiple compound borders. It’s easy to get carried away and create unattractive ones.

A nice use of the compound border is when a matte border is not desired on each side of the component. The matte border and an empty border can be combined to provide this functionality, while keeping the border symmetric around the component.


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.