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:

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

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, where only the top card is visible at any time, that's why it is known as the Card Layout Manager. Each card is normally a panel that can use any layout manager.

Example
  1. import javax.swing.*;  
  2. import java.awt.*;  
  3. import java.awt.event.*;  
  4.   
  5. public class Card extends JFrame implements ActionListener {  
  6.    private CardLayout cardManager;  
  7.    private JPanel deck;  
  8.    private JButton controls[];  
  9.    private String names[] = { "First card""Next card""Previous card""Last card" };  
  10.   
  11.    public Card()  
  12.    {  
  13.       super"CardLayout manager" );  
  14.   
  15.       Container c = getContentPane();  
  16.   
  17.       // create the JPanel with CardLayout  
  18.       deck = new JPanel();  
  19.       cardManager = new CardLayout();  
  20.       deck.setLayout( cardManager );  
  21.   
  22.       // set up card1 and add it to JPanel deck  
  23.       JLabel label1 = new JLabel( "card one", SwingConstants.CENTER );  
  24.       JPanel card1 = new JPanel();  
  25.       card1.setBackground( Color.GREEN);  
  26.       card1.add( label1 );  
  27.       deck.add( card1, label1.getText() ); // add card to deck  
  28.   
  29.       // set up card2 and add it to JPanel deck  
  30.       JLabel label2 = new JLabel( "card two", SwingConstants.CENTER );  
  31.       JPanel card2 = new JPanel();  
  32.       card2.setBackground( Color.blue );  
  33.       card2.add( label2 );  
  34.       deck.add( card2, label2.getText() ); // add card to deck  
  35.      
  36.       // set up card3 and add it to JPanel deck  
  37.       JLabel label3 = new JLabel( "card three" ,SwingConstants.CENTER);  
  38.       JPanel card3 = new JPanel();  
  39.       card3.setBackground( Color.RED);  
  40.       card3.add( label3);  
  41.       deck.add( card3, label3.getText() ); // add card to deck  
  42.      
  43.       // create and layout buttons that will control deck  
  44.       JPanel buttons = new JPanel();  
  45.       buttons.setLayout( new GridLayout( 22 ) );  
  46.       controls = new JButton[ names.length ];  
  47.      
  48.       for ( int i = 0; i < controls.length; i++ ) {  
  49.          controls[ i ] = new JButton( names[ i ] );  
  50.          controls[ i ].addActionListener( this );  
  51.          buttons.add( controls[ i ] );  
  52.       }  
  53.      
  54.       // add JPanel deck and JPanel buttons to the applet  
  55.       c.add( buttons, BorderLayout.WEST );  
  56.       c.add( deck, BorderLayout.EAST );  
  57.      
  58.       setSize( 450,300 );  
  59.       show();  
  60.    }  
  61.      
  62.    public void actionPerformed( ActionEvent e )  
  63.    {  
  64.       if ( e.getSource() == controls[ 0 ] )  
  65.       cardManager.first( deck ); // show first card  
  66.       else if ( e.getSource() == controls[ 1 ] )  
  67.       cardManager.next( deck ); // show next card  
  68.       else if ( e.getSource() == controls[ 2 ] )  
  69.       cardManager.previous( deck ); // show previous card  
  70.       else if ( e.getSource() == controls[ 3 ] )  
  71.       cardManager.last( deck ); // show last card  
  72.    }  
  73.      
  74.    public static void main( String args[] )  
  75.    {  
  76.       Card cardDeckDemo = new Card();  
  77.      
  78.       cardDeckDemo.addWindowListener(  
  79.       new WindowAdapter() {  
  80.          public void windowClosing( WindowEvent e )  
  81.          {  
  82.             System.exit( 0 );  
  83.          }  
  84.       });  
  85.    }  
  86. }  
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

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.   
  5. public class GridBag extends JFrame {  
  6.    private Container container;  
  7.    private GridBagLayout gbLayout;  
  8.    private GridBagConstraints gbConstraints;  
  9.      
  10.    public GridBag()  
  11.    {  
  12.       super"GridBagLayout manager" );  
  13.   
  14.       container = getContentPane();  
  15.       gbLayout = new GridBagLayout();  
  16.       container.setLayout( gbLayout );  
  17.   
  18.       // instantiate gridbag constraints  
  19.       gbConstraints = new GridBagConstraints();  
  20.   
  21.       JTextArea ta = new JTextArea( "TextField1"510 );  
  22.       JTextArea tx = new JTextArea( "TextField2"22 );  
  23.       String names[] = { "Laptop""Palmtop""Tablet""Mobile" };  
  24.       JComboBox cb = new JComboBox( names );  
  25.       JTextField tf = new JTextField( "TextField" );  
  26.       JButton b1 = new JButton( "Button 1" );  
  27.       JButton b2 = new JButton( "Button 2" );  
  28.       JButton b3 = new JButton( "Button 3" );  
  29.         
  30.       // text area  
  31.       // weightx and weighty are both 0: the default  
  32.       // anchor for all components is CENTER: the default  
  33.       gbConstraints.fill = GridBagConstraints.BOTH;  
  34.       addComponent( ta, 0013 );  
  35.         
  36.       // button b1  
  37.       // weightx and weighty are both 0: the default  
  38.       gbConstraints.fill = GridBagConstraints.HORIZONTAL;  
  39.       addComponent( b1, 0121 );  
  40.         
  41.       // combo box  
  42.       // weightx and weighty are both 0: the default  
  43.       // fill is HORIZONTAL  
  44.       addComponent( cb, 2121 );  
  45.         
  46.       // button b2  
  47.       gbConstraints.weightx = 1000// can grow wider  
  48.       gbConstraints.weighty = 1// can grow taller  
  49.       gbConstraints.fill = GridBagConstraints.BOTH;  
  50.       addComponent( b2, 1111 );  
  51.         
  52.       // button b3  
  53.       // fill is BOTH  
  54.       gbConstraints.weightx = 0;  
  55.       gbConstraints.weighty = 0;  
  56.       addComponent( b3, 1211 );  
  57.         
  58.       // textfield  
  59.       // weightx and weighty are both 0: fill is BOTH  
  60.       addComponent( tf, 3021 );  
  61.         
  62.       // textarea  
  63.       // weightx and weighty are both 0: fill is BOTH  
  64.       addComponent( tx, 3211 );  
  65.         
  66.       setSize( 350200 );  
  67.       show();  
  68.    }  
  69.   
  70.    // addComponent is programmer defined  
  71.    private void addComponent( Component c, int row, int column, int width, int height )  
  72.    {  
  73.       // set gridx and gridy  
  74.       gbConstraints.gridx = column;  
  75.       gbConstraints.gridy = row;  
  76.         
  77.       // set gridwidth and gridheight  
  78.       gbConstraints.gridwidth = width;  
  79.       gbConstraints.gridheight = height;  
  80.         
  81.       // set constraints  
  82.       gbLayout.setConstraints( c, gbConstraints );  
  83.       container.add( c ); // add component  
  84.    }  
  85.   
  86.    public static void main( String args[] )  
  87.    {  
  88.       GridBag app = new GridBag();  
  89.         
  90.       app.addWindowListener(  
  91.       new WindowAdapter() {  
  92.          public void windowClosing( WindowEvent e )  
  93.          {  
  94.             System.exit( 0 );  
  95.          }  
  96.       });  
  97.    }  
  98. }  
Output

GridBag Layout Manager

New GridBag Layout Manager

For further read about the topic , please go through the below link.

 

Up Next
    Ebook Download
    View all
    Learn
    View all