Layout Managers in Java : Part 3

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.

http://www.c-sharpcorner.com/UploadFile/9a9e6f/layout-managers-in-java-part-2/

Now let's discuss further about 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.   
  5. public class BoxLayoutDemo extends JFrame {  
  6.    public BoxLayoutDemo()  
  7.    {  
  8.       super( "Demostrating BoxLayout" );  
  9.       final int SIZE = 3;  
  10.   
  11.       Container c = getContentPane();  
  12.       c.setLayout( new BorderLayout( 30, 30 ) );  
  13.   
  14.       Box boxes[] = new Box[ 4 ];  
  15.   
  16.       boxes[ 0 ] = Box.createHorizontalBox();  
  17.       boxes[ 1 ] = Box.createVerticalBox();  
  18.       boxes[ 2 ] = Box.createHorizontalBox();  
  19.       boxes[ 3 ] = Box.createVerticalBox();  
  20.   
  21.       // add buttons to boxes[ 0 ]  
  22.       for ( int i = 0; i < SIZE; i++ )  
  23.          boxes[ 0 ].add( new JButton( "boxes[0]: " + i ) );  
  24.   
  25.       // create strut and add buttons to boxes[ 1 ]  
  26.       for ( int i = 0; i < SIZE; i++ ) {  
  27.          boxes[ 1 ].add( Box.createVerticalStrut( 25 ) );  
  28.          boxes[ 1 ].add( new JButton( "boxes[1]: " + i ) );  
  29.       }  
  30.   
  31.       // create horizontal glue and add buttons to boxes[ 2 ]  
  32.       for ( int i = 0; i < SIZE; i++ ) {  
  33.          boxes[ 2 ].add( Box.createHorizontalGlue() );  
  34.          boxes[ 2 ].add( new JButton( "boxes[2]: " + i ) );  
  35.       }  
  36.   
  37.       // create rigid area and add buttons to boxes[ 3 ]  
  38.       for ( int i = 0; i < SIZE; i++ ) {  
  39.          boxes[ 3 ].add(  
  40.             Box.createRigidArea( new Dimension( 12, 8 ) ) );  
  41.          boxes[ 3 ].add( new JButton( "boxes[3]: " + i ) );  
  42.       }  
  43.   
  44.       // create horizontal glue and add buttons to panel  
  45.       JPanel panel = new JPanel();  
  46.       panel.setLayout(  
  47.          new BoxLayout( panel, BoxLayout.Y_AXIS ) );   // Constructor of Boxlayout class  
  48.   
  49.       for ( int i = 0; i < SIZE; i++ ) {  
  50.          panel.add( Box.createGlue() );  
  51.          panel.add( new JButton( "panel: " + i ) );  
  52.       }  
  53.   
  54.       // place panels on frame  
  55.       c.add( boxes[ 0 ], BorderLayout.NORTH );  
  56.       c.add( boxes[ 1 ], BorderLayout.EAST );  
  57.       c.add( boxes[ 2 ], BorderLayout.SOUTH );  
  58.       c.add( boxes[ 3 ], BorderLayout.WEST );  
  59.       c.add( panel, BorderLayout.CENTER );  
  60.   
  61.       setSize( 400, 400 );  
  62.       show();  
  63.    }  
  64.   
  65.    public static void main( String args[] )  
  66.    {  
  67.       BoxLayoutDemo app = new BoxLayoutDemo();  
  68.   
  69.       app.addWindowListener(  
  70.          new WindowAdapter() {  
  71.             public void windowClosing( WindowEvent e )  
  72.             {  
  73.                System.exit( 0 );  
  74.             }  
  75.          }  
  76.       );  
  77.    }  
  78. }  
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:

new BoxLayout( panel, BoxLayout.Y_AXIS ));
                             arrow
new BoxLayout( panel, BoxLayout.X_AXIS ));

Output:

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.   
  5. public class GroupLayoutDemo {  
  6.    private JFrame mainFrame;  
  7.    private JLabel headerLabel;  
  8.    private JLabel statusLabel;  
  9.    private JPanel controlPanel;  
  10.    private JLabel msglabel;  
  11.   
  12.    public GroupLayoutDemo(){  
  13.       prepareGUI();  
  14.    }  
  15.       public static void main(String[] args){  
  16.       GroupLayoutDemo swingLayoutDemo = new GroupLayoutDemo();  
  17.       swingLayoutDemo.showGroupLayoutDemo();  
  18.    }  
  19.   
  20.    private void prepareGUI(){  
  21.       mainFrame = new JFrame("GroupLayout Manager");  
  22.       mainFrame.setSize(400,400);  
  23.       mainFrame.setLayout(new GridLayout(3, 1));  
  24.   
  25.       headerLabel = new JLabel("",JLabel.CENTER );  
  26.       statusLabel = new JLabel("",JLabel.CENTER);  
  27.   
  28.       statusLabel.setSize(350,100);  
  29.       mainFrame.addWindowListener(new WindowAdapter() {  
  30.          public void windowClosing(WindowEvent windowEvent){  
  31.             System.exit(0);  
  32.          }  
  33.       });  
  34.       controlPanel = new JPanel();  
  35.       controlPanel.setLayout(new FlowLayout());  
  36.   
  37.       mainFrame.add(headerLabel);  
  38.       mainFrame.add(controlPanel);  
  39.       mainFrame.add(statusLabel);  
  40.       mainFrame.setVisible(true);  
  41.    }  
  42.     private void showGroupLayoutDemo(){  
  43.       headerLabel.setText("Demonstration: GroupLayout Manager");  
  44.   
  45.       JPanel panel = new JPanel();  
  46.       // panel.setBackground(Color.darkGray);  
  47.       panel.setSize(200,200);  
  48.       GroupLayout layout = new GroupLayout(panel);  
  49.       layout.setAutoCreateGaps(true);  
  50.       layout.setAutoCreateContainerGaps(true);  
  51.       JButton btn1 = new JButton("Button 1");  
  52.       JButton btn2 = new JButton("Button 2");  
  53.       JButton btn3 = new JButton("Button 3");  
  54.   
  55.       layout.setHorizontalGroup(layout.createSequentialGroup()  
  56.          .addComponent(btn1)  
  57.          .addGroup(layout.createSequentialGroup()  
  58.             .addGroup(layout.createParallelGroup(  
  59.                GroupLayout.Alignment.CENTER)  
  60.                .addComponent(btn2)  
  61.                )  
  62.               )  
  63.                .addGroup(layout.createSequentialGroup()  
  64.             .addGroup(layout.createParallelGroup(  
  65.                GroupLayout.Alignment.CENTER)  
  66.                .addComponent(btn3)  
  67.           )  
  68.          )  
  69.       );  
  70.         layout.setVerticalGroup(layout.createSequentialGroup()  
  71.          .addComponent(btn1)  
  72.          .addComponent(btn2)  
  73.          .addComponent(btn3)  
  74.       );  
  75.       panel.setLayout(layout);  
  76.       controlPanel.add(panel);  
  77.   
  78.       mainFrame.setVisible(true);  
  79.    }  
  80. }  
Output:

Group Layout manager


Wait for further content…

 

Up Next
    Ebook Download
    View all
    Learn
    View all