13.1 Introducing Borders


Figure 13-1 shows the standard borders that Swing provides. There are eight border styles: bevel, soft bevel, empty, etched, line, matte, titled, and compound. The MatteBorder gives you two borders in one: the border area can be filled with either a solid color or an icon. (This figure shows only the icon; you can see a better example of both in Figure 13-11.)

Figure 13-1. Borders in Swing
figs/swng2.1301.gif

You can place a border around any Swing component that extends JComponent. The JComponent class contains a border property that is inherited by all Swing components. (Top-level components that don't inherit from JComponent, like JFrame and JDialog, can't have borders.) By default, the border property is null (no border), but you can access and modify it. Once you've set a component's border, the component paints itself using that border from that point on, and the insets of the border replace the component's default insets.

Here's how to set a component's border:

JLabel label = new JLabel("A Border"); mylabel.setBorder(new BevelBorder(BevelBorder.LOWERED));

Borders are grouped into a separate package within the Swing hierarchy, javax.swing.border. Figure 13-2 shows the classes within this package. The borders included with Swing directly or indirectly extend the AbstractBorder class, which in turn implements the fundamental Border interface and provides a number of helpful housekeeping methods that any implementation can use. (This is an example of the useful skeletal implementation pattern for working with interfaces described in Joshua Bloch's outstanding Effective Java Programming Language Guide [Addison-Wesley].) You'll almost certainly want to use the same technique if you develop your own border.

Figure 13-2. Border class diagram
figs/swng2.1302.gif

Borders can be combined to form more elaborate compound borders. The lower-right corner of Figure 13-1 shows an example of a compound border. We combined an etched border (on the inside) with a raised bevel border (on the outside). Swing allows you to mix any number of border styles into a single border object. This gives Swing borders a useful compositional feature not often found in other graphical toolkits.

13.1.1 The Border Interface

The Border interface contains three methods.

13.1.1.1 Methods
public abstract void paintBorder(Component c, Graphics g, int x, int y, int width, int height)

Draw the border. The border is drawn onto the graphics context g with the location and dimensions provided. paintBorder( ) must calculate exactly what area it can paint by checking the border's insets.

public abstract Insets getBorderInsets(Component c)

Return an Insets object that reports the minimum amount of space the border needs to paint itself around the given component. Borders must never paint outside this insets region (shown shaded in Figure 13-3). When the border property is set, it replaces the native insets of the component with those of the border.

Figure 13-3. A border is allowed to paint itself only within the insets it declares
figs/swng2.1303.gif
public abstract boolean isBorderOpaque( )

Return a boolean indicating whether the border is opaque. Just as components can be opaque or transparent, so can their borders. Opaque borders typically fill the entire border area (the shaded region in Figure 13-3), erasing any contents drawn there previously. Nonopaque borders draw in only part of the region given by their insets and let the rest show through. In the areas left untouched, the background graphics of the bordered component are preserved. Note that if a border returns true for its opaque property, Swing expects it to paint every pixel assigned to it.



Java Swing
Graphic Java 2: Mastering the Jfc, By Geary, 3Rd Edition, Volume 2: Swing
ISBN: 0130796670
EAN: 2147483647
Year: 2001
Pages: 289
Authors: David Geary

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net