Java Reference
In-Depth Information
button.setPreferredSize(size);
// Add top and bottom panel to content pane
Container content = aWindow.getContentPane(); // Get content pane
content.setLayout(new BorderLayout());
// Set border
layout manager
content.add(top, BorderLayout.CENTER);
content.add(bottomPanel, BorderLayout.SOUTH);
aWindow.pack(); // Size for
components
aWindow.setVisible(true); // Display the
window
}
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
public void run() {
createWindow();
}
});
}
}
TryBoxLayout.java
When you run this example and try out the radio buttons and checkboxes, it should produce a window
something like the one shown in Figure 17-22 .
FIGURE 17-22
It's not an ideal arrangement, but you'll improve on it later.
How It Works
The bold code is of interest — the rest you have seen before. The first bold block in createWindow()
creates the left column of radio buttons providing a color choice. You use a Box object with a vertical
orientation to contain the radio buttons. If you try clicking the radio buttons, you find that only one of
them can be selected at a time. Each time you click on a radio button to select it, any other button that is
selected becomes deselected. This is the effect of the ButtonGroup object. To ensure that radio buttons
operate properly, you must add them to a ButtonGroup object.
Note that a ButtonGroup object is not a component — it's just a logical grouping of radio buttons — so
you can't add it to a container. You must add the buttons to the Box container that manages their physical
 
 
Search WWH ::




Custom Search