Brought to you by EarthWeb
ITKnowledge Logo Login Graphic Click here for Oracle 8i Internet Seminars.
Click here for Oracle 8i Internet Seminars.
ITKnowledge
Find:
 
EXPERT SEARCH ----- nav

EarthWeb Direct

EarthWeb Direct

EarthWeb sites: other sites

Previous Table of Contents Next


LineBorder

The static BorderFactory method createLineBorder will return an instance of a LineBorder. As its name implies, a simple line represents a line border. The color of the border must be specified. Optionally, a thickness for the border can be specified. If the thickness is omitted, a border with a thickness of one pixel is returned. The following application demonstrates how to obtain a blue LineBorder with a thickness of five pixels and set it for a JLabel component. This label and its border are shown in Figure 5.3.


Note:  
This first example gives the complete listing for an application that creates the desired border and displays it in an ApplicationFrame. The ApplicationFrame class is presented in Chapter 8, “Frame Windows.” The remaining examples in this section only present the code to create the label and set its border to the desired type. However, the application listed here can be used as the framework for testing the other borders.
package com.foley.test;

import java.awt.*;

import javax.swing.*;
import javax.swing.border.*;

import com.foley.utility.ApplicationFrame;

/**
 * An application that displays a border
 * around a label in a frame.
 *
 * @author Mike Foley
 **/
public class SimpleBorderTest extends JFrame {

    /**
     * Application entry point.
     * Create the frame, and display it.
     *
     * @param args Command line parameter. Not used.
     **/
    public static void main( String args[] ) {

ApplicationFrame frame =
                         new ApplicationFrame( “Simple Border Test” );

        //
        // Create a label and add the border around the label.
        //
        JLabel label = new JLabel( “Line Border” );
        Border border = BorderFactory.createLineBorder( Color.blue, 5 );
        label.setBorder( border );

        frame.getContentPane().add( label, BorderLayout.CENTER );

        frame.pack();
        frame.setVisible( true );

    } // main

} // SimpleBorderTest


Figure 5.3  A simple LineBorder around a JLabel.

BevelBorder

The static BorderFactory method createBevelBorder returns an instance of the BevelBorder class. A BevelBorder can be used to give a component a raised or sunken look. The next code fragment shows how to obtain a reference to a BevelBorder. As the example shows, the RAISED or LOWERED constant defined in the BevelBorder class is used to set the border type. These borders are then added to instances of the JLabel class. The resulting components are shown in Figure 5.4.

JLabel label = new JLabel( “Lowered Bevel Border” );
Border border = BorderFactory.createBevelBorder( BevelBorder.LOWERED );
label.setBorder( border );

JLabel label = new JLabel( “Raised Bevel Border” );
Border border = BorderFactory.createBevelBorder( BevelBorder.RAISED );
label.setBorder( border );


Figure 5.4  Beveled borders.

The convenience methods createRaisedBevelBorder and createLoweredBevelBorder are provided in BorderFactory to create a raised or lowered bevel border without requiring a parameter. By using these method, the previous code could be expressed as follows:

JLabel label = new JLabel( “Lowered Bevel Border” );
Border border = BorderFactory.createLoweredBevelBorder();
label.setBorder( border );

JLabel label = new JLabel( “Raised Bevel Border” );
Border border = BorderFactory.createRaisedBevelBorder();
label.setBorder( border );

These bevel borders use the colors specified by the look-and-feel currently in use. The colors contained in the BevelBorder can be specified in the createBevelBorderMethod. You have two options when specifying colors. You can specify the highlight and shadow colors and let the border determine the inner and outer shades for each border. Or you can specify all four colors that the border uses to paint itself. These colors are the highlightOuter, highlightInner, shadowOuter, and shadowInner. An example of using the methods in the BorderFactory to obtain a BevelBorder with an arbitrary color scheme is presented next. Care should be taken when using these methods because it is very easy to create unattractive borders. The various look-and-feels have different color schemes, making this task even more difficult.

Border border = BorderFactory.createBevelBorder(
                      BevelBorder.RAISED, Color.blue, Color.red );
Border border = BorderFactory.createBevelBorder(
                      BevelBorder.LOWERED, Color.blue, Color.red,
                      Color.yellow, Color.gray );

EtchedBorder

The static BorderFactory method createEtchedBorder returns an instance of the EtchedBorder class. Obtaining an etched border from the BorderFactory is demon- strated in the following code. This border, after being added to a JLabel, is shown in Figure 5.5.

JLabel label = new JLabel( “Etched Border” );
Border border = BorderFactory.createEtchedBorder();
label.setBorder( border );


Figure 5.5  EtchedBorder around a JLabel.

The highlight and shadow color used in the etched border can be specified while obtaining an etched border from the BorderFactory, as in the following:

Border border = BorderFactory.createEtchedBorder(
                       highlightColor, shadowColor )


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.