GridBagLayout with constraints : GridBagLayout « Swing JFC « Java






GridBagLayout with constraints

GridBagLayout with constraints
   

import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.awt.event.WindowListener;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;

public class GridBagWithContaints {

  public static void main(String[] args) {
    JFrame f = new JFrame(
        "Demonstrates the use of gridx, gridy,ipadx, ipady and insets constraints");
    JPanel p = new JPanel();

    p.setLayout(new GridBagLayout());
    // creates a constraints object
    GridBagConstraints c = new GridBagConstraints();
    c.insets = new Insets(2, 2, 2, 2); // insets for all components
    c.gridx = 0; // column 0
    c.gridy = 0; // row 0
    c.ipadx = 5; // increases components width by 10 pixels
    c.ipady = 5; // increases components height by 10 pixels
    p.add(new JButton("Java"), c); // constraints passed in
    c.gridx = 1; // column 1
    // c.gridy = 0; // comment out this for reusing the obj
    c.ipadx = 0; // resets the pad to 0
    c.ipady = 0;
    p.add(new JButton("Source"), c);
    c.gridx = 0; // column 0
    c.gridy = 1; // row 1
    p.add(new JButton("and"), c);
    c.gridx = 1; // column 1
    p.add(new JButton("Support."), c);

    WindowListener wndCloser = new WindowAdapter() {
      public void windowClosing(WindowEvent e) {
        System.exit(0);
      }
    };
    f.addWindowListener(wndCloser);

    f.getContentPane().add(p);
    f.setSize(600, 200);
    f.show();
  }
}
           
         
    
    
  








Related examples in the same category

1.GridBagLayout with weightx and weighty constraintsGridBagLayout with weightx and weighty constraints
2.Simplest gridbag layoutSimplest gridbag layout
3.GridBagLayout with weight constraintGridBagLayout with weight constraint
4.GridBagLayout with anchor constraintsGridBagLayout with anchor constraints
5.GridBagLayout with gridwidth and gridheight constraintsGridBagLayout with gridwidth and gridheight constraints
6.GridBagLayout PaneGridBagLayout Pane
7.Work with GridBagConstraints 1
8.Work GridBagConstraints 3
9.Work with GridBagConstraints 2
10.GridBagConstraints.BOTH
11.Create and set a gridbag layout and how to set gridbag constraints
12.Associate the gridbag constraints with the component
13.Place a component at cell location (1,1)
14.Setting the Location of a Component in a GridBagLayout
15.Making a GridBagLayout Fill the Container
16.Getting the Number of Rows and Columns of Cells in a GridBagLayout
17.Setting the Stretchyness of Rows and Columns in a GridBagLayout Using Layout Weights
18.Place a component at (0,1) with a column weight 1 and a row weight of 2
19.Setting Stretchyness of a GridBagLayout Using Fill
20.Make the component only stretch horizontally
21.Make the component only stretch vertically
22.Make the component on stretchable
23.Make the component stretch in both directions
24.Setting the Space around a Component Within the Cell of the GridBagLayout Using Insets
25.A GridBagLayout Example: weightx, weighty
26.Positioning a component in the center of other component using GridbagLayout
27.Work with GridBagConstraints 5
28.A frame that uses a grid bag layout to arrange font selection components