Java Reference
In-Depth Information
You create a component that represents glue by calling the createGlue() method for a Box object.
You then add the glue component to the Box container in the same way as any other component
wherever you want surplus space to be taken up. You can add glue at several positions in a row or
column, and spare space will be distributed between the glue components. We can add glue after the
last component in each column to make all the spare space appear at the end of each column of buttons.
For the radio buttons we can add the statement,
// Statements adding radio buttons to left Box object
left.add(Box.createGlue()); // Glue at the end
and similarly for the right box. The glue component at the end of each column of buttons will take up
all the surplus space in each vertical Box container. This will make the buttons line up at the top.
Running the program with added glue will result in the following application window.
It's better now, but let's put together a final version of the example with some additional embroidery.
Try It Out - Embroidering Boxes
We will use some JPanel objects with a new kind of border to contain the vertical Box containers.
import javax.swing.*;
import java.awt.*;
import javax.swing.border.*;
public class TryBoxLayout {
// The window object
static JFrame aWindow = new JFrame("This is a Box Layout");
public static void main(String[] args) {
// Set up the window as before...
// Create left column of radio buttons with struts and glue as above...
// Create a panel with a titled border to hold the left Box container
JPanel leftPanel = new JPanel(new BorderLayout());
leftPanel.setBorder(new TitledBorder(
Search WWH ::




Custom Search