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
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 ...
Get Java Swing, 2nd Edition now with the O’Reilly learning platform.
O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.