Section 19.4. BoxLayout


19.4. BoxLayout

Most layout managers are part of the java.awt package and were defined back when Java was first released. Swing adds a couple of new general-purpose layout managers in the javax.swing package; one is BoxLayout. This layout manager is useful for creating simple toolbars or vertical button bars. It lays out components in a single row or column. It is similar to FlowLayout but does not wrap components into new rows.

Although you can use BoxLayout directly, Swing includes a handy container called Box that takes care of the details for you. Every Box uses BoxLayout, but you don't really have to worry about it; the Box class includes some very useful methods for laying out components.

You can create a horizontal or vertical box using Box's static methods.

     Container horizontalBox = Box.createHorizontalBox(  );     Container verticalBox = Box.createVerticalBox(  ); 

Once the Box is created, you can just add( ) components as usual:

     Container box = Box.createHorizontalBox(  );     box.add(new JButton("In the")); 

Box includes several other static methods that create special invisible components that can be used to guide the BoxLayout. The first of these is glue; glue is really space between components in the Box. When the Box is resized, glue expands or contracts as more or less space is available. The other special invisible component type is a strut. Like glue, a strut represents space between components, but it doesn't resize.

The following example creates a horizontal Box (shown in Figure 19-6) that includes both glue and struts. Play around by resizing the window to see the effect of the glue and the struts.

     //file: Boxer.java     import java.awt.*;     import java.awt.event.*;     import javax.swing.*;     public class Boxer extends JPanel {       public static void main(String[] args) {         JFrame frame = new JFrame("Boxer");         frame.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );         frame.setSize(250, 250);         frame.setLocation(200, 200);         Container box = Box.createHorizontalBox(  );         box.add(Box.createHorizontalGlue(  ));         box.add(new JButton("In the"));         box.add(Box.createHorizontalGlue(  ));         box.add(new JButton("clearing"));         box.add(Box.createHorizontalStrut(10));         box.add(new JButton("stands"));         box.add(Box.createHorizontalStrut(10));         box.add(new JButton("a"));         box.add(Box.createHorizontalGlue(  ));         box.add(new JButton("boxer"));         box.add(Box.createHorizontalGlue(  ));         frame.getContentPane(  ).add(box, BorderLayout.CENTER);         frame.pack(  );         frame.setVisible(true);       }     } 

Figure 19-6. Using the Box class


Components are added sequentially for display from left to right or top to bottom with optional "glue" or "strut" constraints placed between them. By default, components simply line up one after another with no space between them. "Glue" acts like a spring, allowing its adjacent components to move to occupy the space evenly. A "strut" imposes a fixed space between adjacent components.



    Learning Java
    Learning Java
    ISBN: 0596008732
    EAN: 2147483647
    Year: 2005
    Pages: 262

    flylib.com © 2008-2017.
    If you may any questions please contact us: flylib@qtcs.net