Layout Managers in Java: Part 3

Introduction 

 
This article explains the Box and Group layout managers, in Java.
 

Layout Managers

 
Go through the following link for the layout managers, discussed in the past.
 
Now, let's discuss further layout managers.
 

Box layout manager

 
This layout manager is used to arrange the components, either vertically or horizontally, in a container.  For this, it provides four constants that are found in the javax.swing package.
 
The four constants are as follows:
  1. public static final int X_AXIS
  2. public static final int Y_AXIS
  3. public static final int LINE_AXIS
  4. public static final int PAGE_AXIS
Example (X_AXIS)
  1. import javax.swing.*;  
  2. import java.awt.*;  
  3. import java.awt.event.*;  
  4. public class BoxLayoutDemo extends JFrame {  
  5.  public BoxLayoutDemo() {  
  6.   super("Demostrating BoxLayout");  
  7.   final int SIZE = 3;  
  8.   Container c = getContentPane();  
  9.   c.setLayout(new BorderLayout(3030));  
  10.   Box boxes[] = new Box[4];  
  11.   boxes[0] = Box.createHorizontalBox();  
  12.   boxes[1] = Box.createVerticalBox();  
  13.   boxes[2] = Box.createHorizontalBox();  
  14.   boxes[3] = Box.createVerticalBox();  
  15.   for (int i = 0; i < SIZE; i++)  
  16.    boxes[0].add(new JButton("boxes[0]: " + i));  
  17.   for (int i = 0; i < SIZE; i++) {  
  18.    boxes[1].add(Box.createVerticalStrut(25));  
  19.    boxes[1].add(new JButton("boxes[1]: " + i));  
  20.   }  
  21.   for (int i = 0; i < SIZE; i++) {  
  22.    boxes[2].add(Box.createHorizontalGlue());  
  23.    boxes[2].add(new JButton("boxes[2]: " + i));  
  24.   }  
  25.   for (int i = 0; i < SIZE; i++) {  
  26.    boxes[3].add(Box.createRigidArea(new Dimension(128)));  
  27.    boxes[3].add(new JButton("boxes[3]: " + i));  
  28.   }  
  29.   JPanel panel = new JPanel();  
  30.   panel.setLayout(new BoxLayout(panel, BoxLayout.Y_AXIS));  
  31.   for (int i = 0; i < SIZE; i++) {  
  32.    panel.add(Box.createGlue());  
  33.    panel.add(new JButton("panel: " + i));  
  34.   }  
  35.   c.add(boxes[0], BorderLayout.NORTH);  
  36.   c.add(boxes[1], BorderLayout.EAST);  
  37.   c.add(boxes[2], BorderLayout.SOUTH);  
  38.   c.add(boxes[3], BorderLayout.WEST);  
  39.   c.add(panel, BorderLayout.CENTER);  
  40.   setSize(400400);  
  41.   show();  
  42.  }  
  43.  public static void main(String args[]) {  
  44.   BoxLayoutDemo app = new BoxLayoutDemo();  
  45.   app.addWindowListener(new WindowAdapter() {  
  46.    public void windowClosing(WindowEvent e) {  
  47.     System.exit(0);  
  48.    }  
  49.   });  
  50.  }  
  51. }   
Output
 
Box layout manager 
 
Example (Y_AXIS)
 
For the BoxLayout class with Y_AXIS, we need to edit the line of code, as in the following:
  1. new BoxLayout(panel, BoxLayout.Y_AXIS));  
  2. arrow  
  3. new BoxLayout(panel, BoxLayout.X_AXIS)); 
Output
 
 
BoxLayout class 
 
BoxLayout class 
 

Group Layout manager

 
This layout manager is used to arrange the group components hierarchically, to position them in a container.  This is also found in the javax.swing package.
 
It contains the following two fields: 
  1. static int DEFAULT_SIZE
  2. static int PREFERRED_SIZE
Example
  1. import java.awt.*;  
  2. import java.awt.event.*;  
  3. import javax.swing.*;  
  4. public class GroupLayoutDemo {  
  5.  private JFrame mainFrame;  
  6.  private JLabel headerLabel;  
  7.  private JLabel statusLabel;  
  8.  private JPanel controlPanel;  
  9.  private JLabel msglabel;  
  10.  public GroupLayoutDemo() {  
  11.   prepareGUI();  
  12.  }  
  13.  public static void main(String[] args) {  
  14.   GroupLayoutDemo swingLayoutDemo = new GroupLayoutDemo();  
  15.   swingLayoutDemo.showGroupLayoutDemo();  
  16.  }  
  17.  private void prepareGUI() {  
  18.   mainFrame = new JFrame("GroupLayout Manager");  
  19.   mainFrame.setSize(400400);  
  20.   mainFrame.setLayout(new GridLayout(31));  
  21.   headerLabel = new JLabel("", JLabel.CENTER);  
  22.   statusLabel = new JLabel("", JLabel.CENTER);  
  23.   statusLabel.setSize(350100);  
  24.   mainFrame.addWindowListener(new WindowAdapter() {  
  25.    public void windowClosing(WindowEvent windowEvent) {  
  26.     System.exit(0);  
  27.    }  
  28.   });  
  29.   controlPanel = new JPanel();  
  30.   controlPanel.setLayout(new FlowLayout());  
  31.   mainFrame.add(headerLabel);  
  32.   mainFrame.add(controlPanel);  
  33.   mainFrame.add(statusLabel);  
  34.   mainFrame.setVisible(true);  
  35.  }  
  36.  private void showGroupLayoutDemo() {  
  37.   headerLabel.setText("Demonstration: GroupLayout Manager");  
  38.   JPanel panel = new JPanel();  
  39.   panel.setSize(200200);  
  40.   GroupLayout layout = new GroupLayout(panel);  
  41.   layout.setAutoCreateGaps(true);  
  42.   layout.setAutoCreateContainerGaps(true);  
  43.   JButton btn1 = new JButton("Button 1");  
  44.   JButton btn2 = new JButton("Button 2");  
  45.   JButton btn3 = new JButton("Button 3");  
  46.   layout.setHorizontalGroup(layout.createSequentialGroup().addComponent(btn1).addGroup(layout.createSequentialGroup().addGroup(layout.createParallelGroup(GroupLayout.Alignment.CENTER).addComponent(btn2))).addGroup(layout.createSequentialGroup().addGroup(layout.createParallelGroup(GroupLayout.Alignment.CENTER).addComponent(btn3))));  
  47.   layout.setVerticalGroup(layout.createSequentialGroup().addComponent(btn1).addComponent(btn2).addComponent(btn3));  
  48.   panel.setLayout(layout);  
  49.   controlPanel.add(panel);  
  50.   mainFrame.setVisible(true);  
  51.  }  
  52. }   
Output
 
Group Layout manager 
 
Wait for further content…


Similar Articles