Previous | Next | Trail Map | Creating a User Interface | Using the JFC/Swing Packages

How to Use Borders

Every JComponent can have one or more borders. Borders are incredibly useful objects that, while not themselves components, know how to draw the edges of Swing components. Borders are useful not only for drawing lines and fancy edges, but also for providing titles and empty space around components.

To put a border around a JComponent, you use its setBorder method. You can use the BorderFactory(in the API reference documentation) class to create any of the Swing borders. Here is an example of code that creates a bordered container:

JPanel pane = new JPanel();
pane.setBorder(BorderFactory.createEtchedBorder());

Many of the ready-to-use Swing components use borders to draw the outline of the component. If you want to draw an additional border around an already bordered component -- to provide some extra space above a scroll pane, for example -- then you need to add the new border to the existing border. Here's an example of adding to an existing border:

aScrollPane.setBorder(
    BorderFactory.createCompoundBorder(
                    BorderFactory.createEmptyBorder(20,0,0,0),
                    aScrollPane.getBorder())
);

The new border, returned by createEmptyBorder, adds 20 pixels of empty space above any component that uses it. To combine the new border with the existing border (which is returned by getBorder), the code uses the createCompoundBorder method.

The following pictures show an application that displays the borders Swing provides. The first picture shows some simple borders:

BorderDemo: Simple Borders
The next picture shows titled borders. Using a titled border, you can convert any border into one that displays a text description. By default, the title straddles the upper left of the border, as shown at the top of the following figure.
BorderDemo: Titled Borders
The next picture shows compound borders. With compound borders, you can combine any two borders (which can themselves be compound borders).
BorderDemo: Compound Borders

The following code shows how to create and set the borders shown in the preceding figures. (You can find the program's code in BorderDemo.java.)

//Simple borders

jComp1.setBorder(BorderFactory.createEtchedBorder());

//Keep references to the next few borders, for use with title.
Border blackline, raisedbevel, loweredbevel, empty;

blackline = BorderFactory.createLineBorder(Color.black);
jComp2.setBorder(blackline);

raisedbevel = BorderFactory.createRaisedBevelBorder();
jComp3.setBorder(raisedbevel);

loweredbevel = BorderFactory.createLoweredBevelBorder();
jComp4.setBorder(loweredbevel);

empty = BorderFactory.createEmptyBorder();
jComp5.setBorder(empty);

//Titled borders
TitledBorder title1, title2, title3, title4, title5;
title1 = BorderFactory.createTitledBorder("title");
jComp6.setBorder(title1);

title2 = BorderFactory.createTitledBorder(
		       blackline, "title");
title2.setTitleJustification(TitledBorder.CENTER);
jComp7.setBorder(title2);

title3 = BorderFactory.createTitledBorder(
		       raisedbevel, "title");
title3.setTitleJustification(TitledBorder.RIGHT);
jComp8.setBorder(title3);

title4 = BorderFactory.createTitledBorder(
		       loweredbevel, "title");
title4.setTitlePosition(TitledBorder.ABOVE_TOP);
jComp9.setBorder(title4);

title5 = BorderFactory.createTitledBorder(
		       empty, "title");
title5.setTitlePosition(TitledBorder.BOTTOM);
jComp10.setBorder(title5);

//Compound borders
Border compound1, compound2, compound3;
Border redline = BorderFactory.createLineBorder(Color.red);

//This creates a nice frame.
compound1 = BorderFactory.createCompoundBorder(
			  raisedbevel, loweredbevel);
jComp11.setBorder(compound1);

//Add a red outline to the frame.
compound2 = BorderFactory.createCompoundBorder(
			  redline, compound1);
jComp12.setBorder(compound2);

//Add a title to the red-outlined frame.
compound3 = BorderFactory.createTitledBorder(
			  compound2, "title",
			  TitledBorder.CENTER,
			  TitledBorder.BELOW_BOTTOM);
jComp13.setBorder(compound3);

[PENDING: Put API summary here.]

For more examples of using borders, see [PENDING: LIST HERE].


Previous | Next | Trail Map | Creating a User Interface | Using the JFC/Swing Packages