BorderFactory Class
Package: javax.swing
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.
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.
Methods
Method |
Description |
|
Creates a beveled border of the specified type. The type parameter can be |
|
Creates an empty border that occupies the space indicated by the parameters. |
|
Creates an etched border. |
|
Creates a line border. |
|
Creates a lowered beveled border. |
|
Creates a raised beveled border. |
|
Creates a titled etched border. |
|
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”);