Java Reference
In-Depth Information
More Swing
components
16
In this chapter we introduce more graphical components from the Swing library.
Some of them, such as lists and tables, are used to display data in a structured way.
Others, such as split panes or tabbed panes, are used to embed other components
so that one can easily switch between them.
16.1
Borders
One can draw a line - a border - around most Swing components in order to
separate them from one another. Swing offers class Border and the utility class
BorderFactory for this purpose. There are various different types of borders. We
show only how to get an 'etched border'; for others see the documentation. Let
comp be a Swing component. To get a border, use the appropriate methods of the
class BorderFactory namely:
Border bdr = BorderFactory.createEtchedBorder();
comp.setBorder(bdr);
To create a border with a text, one first creates a border bdr and then adds the
text:
Border bdr = BorderFactory.createEtchedBorder();
Border textbdr = BorderFactory.createTitledBorder(Border bdr,
Stringtext);
Here is an example consisting of four panels with borders, two have a text. The
result is shown in Figure 16.1. Note that the border is inside the panel, so leave
!
a margin if you draw in the panel.
File: its/Borders/BorderPanel.java
1. package its.Borders;
2.
3. import java.awt.*;
4. import javax.swing.JPanel;
Search WWH ::




Custom Search