Layout Managers in Java : Part 2

Introduction

 
This article contains information about Layout Managers, beyond that discussed in Part 1.
 

Layout Managers

 
For previously discussed Layout Managers, follow the link given below: 
 

Card Layout Manager

 
The card layout class manages two or more components that share the same display space, in such a way that only one component is visible at a time.
 
Basically, each card is like a playing card in a stack, in which only the top card is visible at any time.   That is why it is known, as the Card Layout Manager.  Each card is normally a panel that can use any layout manager.
 
Example
  1. import java.awt.*;  
  2. import javax.swing.*;  
  3. public class Flow {  
  4.  JFrame f;  
  5.  public Flow() {  
  6.   f = new JFrame();  
  7.   JButton b1 = new JButton("Red");  
  8.   JButton b2 = new JButton("Green");  
  9.   JButton b3 = new JButton("Yellow");  
  10.   JButton b4 = new JButton("Purple");  
  11.   JButton b5 = new JButton("Blue");  
  12.   JButton b6 = new JButton("Pink");  
  13.   JButton b7 = new JButton("Brown");  
  14.   f.add(b1);  
  15.   f.add(b2);  
  16.   f.add(b3);  
  17.   f.add(b4);  
  18.   f.add(b5);  
  19.   f.add(b6);  
  20.   f.add(b7);  
  21.   f.setLayout(new FlowLayout(FlowLayout.LEFT));  
  22.   f.setSize(400200);  
  23.   f.setVisible(true);  
  24.  }  
  25.  public static void main(String[] args) {  
  26.   new Flow();  
  27.  }  
  28. }   
Output
 
 Card Layout Manager
 
Outputs after clicking "Next card" and "Last card" and then "previous card":
 
Next card
 
Next Card in Layout Manager
 
Last card
 
Last Card in Layout Manager
 
The previous card will be the next card, in other words, card two.
 
Previous Card in Layout Manager
 

GridBag Layout Manager

 
The GridBag layout manages the components in rows or columns that allow specified components to span multiple rows and columns.  Not all the rows and columns have the same height and width.
 
This is the most complex layout manager, since it specifies the size and position characteristics of its components, by specifying constraints for each component.
 
Example
  1. import javax.swing.*;  
  2. import java.awt.*;  
  3. import java.awt.event.*;  
  4. public class GridBag extends JFrame {  
  5.  private Container container;  
  6.  private GridBagLayout gbLayout;  
  7.  private GridBagConstraints gbConstraints;  
  8.  public GridBag() {  
  9.   super("GridBagLayout manager");  
  10.   container = getContentPane();  
  11.   gbLayout = new GridBagLayout();  
  12.   container.setLayout(gbLayout);  
  13.   gbConstraints = new GridBagConstraints();  
  14.   JTextArea ta = new JTextArea("TextField1"510);  
  15.   JTextArea tx = new JTextArea("TextField2"22);  
  16.   String names[] = {  
  17.    "Laptop",  
  18.    "Palmtop",  
  19.    "Tablet",  
  20.    "Mobile"  
  21.   };  
  22.   JComboBox cb = new JComboBox(names);  
  23.   JTextField tf = new JTextField("TextField");  
  24.   JButton b1 = new JButton("Button 1");  
  25.   JButton b2 = new JButton("Button 2");  
  26.   JButton b3 = new JButton("Button 3");  
  27.   gbConstraints.fill = GridBagConstraints.BOTH;  
  28.   addComponent(ta, 0013);  
  29.   gbConstraints.fill = GridBagConstraints.HORIZONTAL;  
  30.   addComponent(b1, 0121);  
  31.   addComponent(cb, 2121);  
  32.   gbConstraints.weightx = 1000;  
  33.   gbConstraints.weighty = 1;  
  34.   gbConstraints.fill = GridBagConstraints.BOTH;  
  35.   addComponent(b2, 1111);  
  36.   gbConstraints.weightx = 0;  
  37.   gbConstraints.weighty = 0;  
  38.   addComponent(b3, 1211);  
  39.   addComponent(tf, 3021);  
  40.   addComponent(tx, 3211);  
  41.   setSize(350200);  
  42.   show();  
  43.  }  
  44.  private void addComponent(Component c, int row, int column, int width, int height) {  
  45.   gbConstraints.gridx = column;  
  46.   gbConstraints.gridy = row;  
  47.   gbConstraints.gridwidth = width;  
  48.   gbConstraints.gridheight = height;  
  49.   gbLayout.setConstraints(c, gbConstraints);  
  50.   container.add(c);  
  51.  }  
  52.  public static void main(String args[]) {  
  53.   GridBag app = new GridBag();  
  54.   app.addWindowListener(new WindowAdapter() {  
  55.    public void windowClosing(WindowEvent e) {  
  56.     System.exit(0);  
  57.    }  
  58.   });  
  59.  }  
  60. }   
Output
 
GridBag Layout Manager
 
New GridBag Layout Manager
 
For further information about the topic, please go through the below link.