![]() ![]() |
![]() |
![]() ![]() ![]() ![]() |
![]() |
In Swing, a border is an object that can be used to draw a border around a component. A border, like an inset, describes a space. But a border, unlike an inset, can actually draw in the space that it describes. Swing provides classes that you can use to draw several different kinds of borders, as shown in the following illustration:
The Border interface defines two methods:
public interface Border { void paintBorder(Graphics g, int x, int y, int width, int height); Insets getBorderInsets(); booolean isBorderOpaque(); }
The paintBorder() method renders the border into the rectangle described by the parameters passed in. This method should draw only in a space that it has asked for by calling the getBorderInsets() method. You should not fill the entire rectangle with a color, as the component may have already been drawing in this area.
The getBorderInsets() method describes the amount of space that a border object needs to paint properly. JComponents will take this information into account when arranging their internal components, making sure that they do not paint into the area that the border is reserving.
The isBorderOpaque() method should return true if the border completely paints its rectangle. Otherwise, false is returned.
For example, suppose you have created a button object named b. You can then create a line border around it by making a call similar to this:
b.setBorder(new JLineBorder(color.black, 2));
This call creates a new JLineBorder instance that paints a two-pixel black border all around the JButton object. This is how the first component was configured in the image drawn in the previous example. The text and icons for the button are sized and positioned in such a way that they fall within the remaining area.
A slightly more complex example uses the JTitledBorder object. It takes another border object as one of its parameters, allowing you to mix and match borders. The following code fragment illustrates how the "JTitledBorder 2" component was created from the image in the preceding example.
b.setBorder(new JTitledBorder(b, new JBezelBorder(JBezelBorder.RAISED), "Using JBezelBorder"));
Swing provides a set of generic border implementations. You can easily create your own Border objects to create many varieties of complicated border drawing operations. By extracting this drawing code into a separate object, you can easily reuse your drawing code in many different components. These default implementations can be found in the com.sun.java.swing.border package.
As shown in the picture at the beginning of this specification, Swing provides several classes that can be used to create and manage several styles of borders:
For more information about each of these kinds of borders, see the Swing
API.
Version 0.4. Last modified 09/04/97.
Copyright © 1995-97 Sun
Microsystems, Inc. All Rights Reserved.