O'Reilly logo

Java Swing, 2nd Edition by Brian Cole, James Elliott, Marc Loy, Robert Eckstein, Dave Wood

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

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.)

Borders in Swing

Figure 13-1. Borders in Swing

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 ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required