BorderFactory Class

Package: javax.swing

tip.eps The BorderFactory class is in the javax.swing package, but the Border interface that defines the resulting border objects is in javax.swing.border.

The BorderFactory class creates decorative borders that are used to visually group components. The frame in Figure 5-1 shows some radio buttons and check boxes inside borders.

9781118239742-fg0501.tif

Figure 5-1

You can apply a border to any object that inherits JComponent, but the usual technique is to apply the border to a panel and add to that panel any components you want to appear within the border.

CrossRef.eps Panels are the usual way to display two or more controls that are visually grouped together. For example, if you want to display several radio buttons together, the usual way is to add the radio buttons to a panel, add a border to the panel, and then add the panel to the frame. For more information, see JPanel Class.

Remember.eps All methods of the BorderFactory class are static. Border Factory has no constructor, so you don’t need to create an instance.

Methods

Method

Description

static Border create BevelBorder(int type)

Creates a beveled border of the specified type. The type parameter can be BevelBorder.LOWERED or BevelBorder.RAISED.

static Border create EmptyBorder(int top, int left, int bottom, int right)

Creates an empty border that occupies the space indicated by the parameters.

static Border create EtchedBorder()

Creates an etched border.

static Border create LineBorder()

Creates a line border.

static Border create LoweredBevelBorder()

Creates a lowered beveled border.

static Border create RaisedBevelBorder()

Creates a raised beveled border.

static Border create TitledBorder(String title)

Creates a titled etched border.

static Border create TitledBorder(Border b, String title)

Creates a titled border from the specified border.

Each static method of the BorderFactory class creates a border with a slightly different visual style. Then you apply the Border object to a panel by calling the panel’s setBorder method.

Here’s a snippet of code that creates a panel, creates a titled border, and applies the border to the panel:

JPanel sizePanel = new JPanel();

Border b1 = BorderFactory.createTitledBorder(“Size”);

sizePanel.setBorder(b1);

Any components you add to sizePanel appear within this border.

The last method listed in Methods table for this class

static Border createTitledBorder(Border b, String title)

needs a little explanation. It simply adds a title to a border created by any of the other created methods of the BorderFactory class. You can create a raised beveled border with the title Options like this:

Border b = BorderFactory.createRaisedBevelBorder();

b = BorderFactory.createTitledBorder(b, “Options”);

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset