Demonstrating GridBagLayout constants. - Java Swing

Java examples for Swing:GridBagLayout

Description

Demonstrating GridBagLayout constants.

Demo Code

import javax.swing.JFrame;

import java.awt.GridBagLayout;
import java.awt.GridBagConstraints;
import java.awt.Component;
import javax.swing.JFrame;
import javax.swing.JComboBox;
import javax.swing.JTextField;
import javax.swing.JList;
import javax.swing.JButton;
class GridBagFrame2 extends JFrame 
{ 
   private final GridBagLayout layout; // layout of this frame
   private final GridBagConstraints constraints; // layout's constraints
    /*from  w w w  . ja va  2  s.  co m*/
   public GridBagFrame2()
   {
      layout = new GridBagLayout();
      setLayout(layout);
      constraints = new GridBagConstraints(); // instantiate constraints

      // create GUI components
      String[] metals = {"Copper", "Aluminum", "Silver"};
      JComboBox<String> comboBox = new JComboBox<String>(metals);

      JTextField textField = new JTextField("TextField");

      String[] fonts = {"Serif", "Monospaced"};
      JList list = new JList(fonts);

      String[] names = {"zero", "one", "two", "three", "four"};
      JButton[] buttons = new JButton[names.length];

      for (int count = 0; count < buttons.length; count++)
         buttons[count] = new JButton(names[count]);

      // define GUI component constraints for textField
      constraints.weightx = 1;
      constraints.weighty = 1;  
      constraints.fill = GridBagConstraints.BOTH;
      constraints.gridwidth = GridBagConstraints.REMAINDER;
      addComponent(textField);

      // buttons[0] -- weightx and weighty are 1: fill is BOTH
      constraints.gridwidth = 1;
      addComponent(buttons[0]);

      // buttons[1] -- weightx and weighty are 1: fill is BOTH
      constraints.gridwidth = GridBagConstraints.RELATIVE;
      addComponent(buttons[1]);

      // buttons[2] -- weightx and weighty are 1: fill is BOTH
      constraints.gridwidth = GridBagConstraints.REMAINDER;
      addComponent(buttons[2]);

      // comboBox -- weightx is 1: fill is BOTH
      constraints.weighty = 0;
      constraints.gridwidth = GridBagConstraints.REMAINDER;
      addComponent(comboBox);
      
      // buttons[3] -- weightx is 1: fill is BOTH
      constraints.weighty = 1;
      constraints.gridwidth = GridBagConstraints.REMAINDER;
      addComponent(buttons[3]); 

      // buttons[4] -- weightx and weighty are 1: fill is BOTH
      constraints.gridwidth = GridBagConstraints.RELATIVE;
      addComponent(buttons[4]);

      // list -- weightx and weighty are 1: fill is BOTH
      constraints.gridwidth = GridBagConstraints.REMAINDER;
      addComponent(list);
   }

   private void addComponent(Component component) 
   {
      layout.setConstraints(component, constraints);
      add(component); // add component
   } 
}

public class Main
{ 
   public static void main(String[] args)
   { 
      GridBagFrame2 gridBagFrame = new GridBagFrame2();  
      gridBagFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
      gridBagFrame.setSize(300, 200); 
      gridBagFrame.setVisible(true); 
   } 
}

Related Tutorials