Java Reference
In-Depth Information
Note The Factory design pattern, used by the BorderFactory , is often used when many similar objects
need to be created, but the user of the objects does not need to know the implementation details—all the
user of the object needs to know about is how to use it. Each of the BorderFactory 's create methods will
create a Border , where Border is an interface. Since we have an interface for all potential borders, we can
use the created border in any JFrame or JPanel without concerning ourselves with what type of border was
created. Furthermore, we don't have to worry about how a particular border will be created on different
operating systems—the factory handles all that for us.
Which particular border to create and use is up to you. Frequently status bars use a low-
ered bevel border (which you can create using the createLoweredBevelBorder method),
groupings of radio buttons use a titled border ( createTitledBorder ), and groupings of buttons
might be enclosed in a beveled border without a title ( createBevelBorder ).
An example of creating a titled border around a JComboBox is shown in Listing 8-7, with the
window created shown in Figure 8-18. For this example we included some spacing labels to
make the border more obvious.
Listing 8-7. Demonstration of the JComboBox Component and the BorderFactory
import java.awt.*;
import javax.swing.*;
public class MyFrame extends JFrame {
private final static String TITLE = "Title goes here";
public static void main(String[] args) throws Exception {
new MyFrame().setVisible(true);
}
public MyFrame() throws Exception {
setDefaultCloseOperation(EXIT_ON_CLOSE);
String[] items = {"One", "Two", "Three", "Four", "Five"};
JComboBox choosableItems = new JComboBox(items);
JPanel clientServerPanel = new JPanel();
clientServerPanel.setBorder(BorderFactory.createTitledBorder(TITLE));
clientServerPanel.add(new JLabel("Pick a number:"), BorderLayout.EAST);
clientServerPanel.add(choosableItems, BorderLayout.CENTER);
this.add(new JLabel(" "), BorderLayout.NORTH);
this.add(new JLabel(" "), BorderLayout.SOUTH);
this.add(new JLabel(" "), BorderLayout.EAST);
Search WWH ::




Custom Search