Java GridBagConstraints RELATIVE

Syntax

GridBagConstraints.RELATIVE has the following syntax.

public static final int RELATIVE

Example

In the following code shows how to use GridBagConstraints.RELATIVE field.


/*from   w w  w  . j a v a  2  s.  c o m*/
import java.awt.Container;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;

import javax.swing.BorderFactory;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.Border;

public class Main {

  public static void main(String[] args) {
    JFrame aWindow = new JFrame();
    aWindow.setBounds(200, 200, 200, 200);
    aWindow.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

    Container content = aWindow.getContentPane();
    content.add(new GridBagLayoutPanel());
    aWindow.setVisible(true);
  }
}

class GridBagLayoutPanel extends JPanel {

  public GridBagLayoutPanel() {
    GridBagLayout gridbag = new GridBagLayout();
    GridBagConstraints constraints = new GridBagConstraints();
    setLayout(gridbag);

    constraints.weightx = constraints.weighty = 10.0;
    constraints.fill = constraints.NONE;
    constraints.ipadx = 30;
    constraints.ipady = 10;
    addButton("Press", constraints, gridbag);

    constraints.weightx = 5.0;
    constraints.fill = constraints.BOTH;
    constraints.ipadx = constraints.ipady = 0;
    constraints.insets = new Insets(10, 30, 10, 20);
    constraints.gridwidth = constraints.RELATIVE;
    constraints.gridheight = 2;
    addButton("GO", constraints, gridbag);

    constraints.insets = new Insets(0, 0, 0, 0);
    constraints.gridx = 0;
    constraints.fill = constraints.NONE;
    constraints.ipadx = 30;
    constraints.ipady = 10;
    constraints.gridwidth = 1;
    constraints.gridheight = 1;
    addButton("Push", constraints, gridbag);

  }

  private void addButton(String label, GridBagConstraints constraints,
      GridBagLayout layout) {

    Border edge = BorderFactory.createRaisedBevelBorder();

    JButton button = new JButton(label);
    button.setBorder(edge);
    layout.setConstraints(button, constraints);
    add(button);
  }
}

           




















Home »
  Java Tutorial »
    java.awt »




BasicStroke
BorderLayout
CardLayout
Color
Cursor
Desktop
DesktopManager
DisplayMode
EventQueue
FlowLayout
FocusTraversalPolicy
Font
FontMetrics
GradientPaint
Graphics
Graphics2D
GraphicsConfiguration
GraphicsDevice
GraphicsEnvironment
GridBagConstraints
GridBagLayout
GridLayout
Image
ItemSelectable
KeyboardFocusManager
LayoutManager
LayoutManager2
Point
Rectangle
Robot
Shape
SplashScreen
SystemColor
SystemTray
TexturePaint
TrayIcon
Toolkit
Transparency