Tabnine Logo
GridBagLayout
Code IndexAdd Tabnine to your IDE (free)

How to use
GridBagLayout
in
java.awt

Best Java code snippets using java.awt.GridBagLayout (Showing top 20 results out of 7,425)

Refine searchRefine arrow

  • GridBagConstraints
  • JPanel
  • JLabel
  • Insets
  • Container
  • JFrame
  • Window
  • JButton
origin: stanfordnlp/CoreNLP

private JPanel makeBrowseButtonBox() {
 JPanel buttonBox = new JPanel();
 buttonBox.setComponentOrientation(ComponentOrientation.RIGHT_TO_LEFT);
 buttonBox.setLayout(new GridBagLayout());
 browseButton = new JButton("Browse Trees");
 browseButton.addActionListener(this);
 JLabel sizeLabel = new JLabel("Tree size:");
 JSlider fontSlider = new JSlider(2, 64, 12);
 fontSlider.addChangeListener(this);
 GridBagConstraints buttonConstraints = new GridBagConstraints();
 buttonConstraints.fill = GridBagConstraints.HORIZONTAL;
 buttonConstraints.weightx = 0.2;
 buttonConstraints.weighty = 0.2;
 buttonBox.add(browseButton,buttonConstraints);
 buttonConstraints.weightx = 0.6;
 buttonBox.add(fontSlider, buttonConstraints);
 buttonConstraints.weightx = 0.2;
 buttonBox.add(sizeLabel, buttonConstraints);
 return buttonBox;
}
origin: libgdx/libgdx

  public void actionPerformed (ActionEvent event) {
    boolean visible = !highMaxSlider.isVisible();
    highMaxSlider.setVisible(visible);
    highRangeButton.setText(visible ? "<" : ">");
    GridBagLayout layout = (GridBagLayout)formPanel.getLayout();
    GridBagConstraints constraints = layout.getConstraints(highRangeButton);
    constraints.gridx = visible ? 5 : 4;
    layout.setConstraints(highRangeButton, constraints);
    Slider slider = visible ? highMaxSlider : highMinSlider;
    value.setHighMax((Float)slider.getValue());
  }
});
origin: libgdx/libgdx

public void setEmbedded () {
  GridBagLayout layout = (GridBagLayout)getLayout();
  GridBagConstraints constraints = layout.getConstraints(contentPanel);
  constraints.insets = new Insets(0, 0, 0, 0);
  layout.setConstraints(contentPanel, constraints);
  titlePanel.setVisible(false);
}
origin: libgdx/libgdx

private void initializeComponents () {
  getContentPane().setLayout(new GridBagLayout());
  JPanel leftSidePanel = new JPanel();
  leftSidePanel.setLayout(new GridBagLayout());
  getContentPane().add(leftSidePanel, new GridBagConstraints(0, 1, 1, 1, 1.0, 1.0, GridBagConstraints.CENTER,
    GridBagConstraints.BOTH, new Insets(0, 0, 0, 0), 0, 0));
    JPanel fontPanel = new JPanel();
    leftSidePanel.add(fontPanel, new GridBagConstraints(0, 0, 1, 1, 0.0, 0.0, GridBagConstraints.CENTER,
      GridBagConstraints.BOTH, new Insets(5, 5, 5, 5), 0, 0));
    fontPanel.setLayout(new GridBagLayout());
      unicodePanel = new JPanel(new GridBagLayout());
      bitmapPanel = new JPanel(new GridBagLayout());
      JPanel renderingPanel = new JPanel(new GridBagLayout());
    samplePanel.setLayout(new GridBagLayout());
    renderingPanel.setLayout(new GridBagLayout());
      glyphCachePanel.setLayout(new GridBagLayout());
      radioButtonsPanel.setLayout(new GridBagLayout());
  rightSidePanel.setLayout(new GridBagLayout());
  getContentPane().add(rightSidePanel, new GridBagConstraints(1, 0, 1, 2, 0.0, 0.0, GridBagConstraints.CENTER,
    paddingPanel.setLayout(new GridBagLayout());
origin: igniterealtime/Openfire

frame.setTitle(appName);
JPanel mainPanel = new JPanel();
JLabel splashLabel = null;
cardPanel.setLayout(cardLayout);
  splashLabel = new JLabel("", splash, JLabel.CENTER);
  frame.setIconImage(offIcon.getImage());
mainPanel.setLayout(new BorderLayout());
cardPanel.setBackground(Color.white);
final JButton startButton = new JButton("Start");
startButton.setActionCommand("Start");
quitButton.setActionCommand("Quit");
toolbar.setLayout(new GridBagLayout());
toolbar.add(startButton, new GridBagConstraints(0, 0, 1, 1, 0.0, 0.0,
    GridBagConstraints.WEST, GridBagConstraints.NONE, new Insets(5, 5, 5, 5), 0, 0));
toolbar.add(stopButton, new GridBagConstraints(1, 0, 1, 1, 0.0, 0.0,
    GridBagConstraints.WEST, GridBagConstraints.NONE, new Insets(5, 5, 5, 5), 0, 0));
toolbar.add(browserButton, new GridBagConstraints(2, 0, 1, 1, 0.0, 0.0,
    GridBagConstraints.WEST, GridBagConstraints.NONE, new Insets(5, 5, 5, 5), 0, 0));
toolbar.add(quitButton, new GridBagConstraints(3, 0, 1, 1, 0.0, 0.0,
origin: libgdx/libgdx

effectsList.getListSelectionListeners()[0].valueChanged(null);
setLayout(new GridBagLayout());
setBorder(BorderFactory.createMatteBorder(0, 0, 1, 0, java.awt.Color.black));
appliedEffectsPanel.add(this, constrains);
  JPanel titlePanel = new JPanel();
  titlePanel.setLayout(new LayoutManager() {
    public void removeLayoutComponent (Component comp) {
    upButton = new JButton();
    titlePanel.add(upButton);
    upButton.setText("Up");
    upButton.setMargin(new Insets(0, 0, 0, 0));
    downButton.setMargin(new Insets(0, 0, 0, 0));
    deleteButton.setMargin(new Insets(0, 0, 0, 0));
    nameLabel = new JLabel(effect.toString());
    titlePanel.add(nameLabel);
    Font font = nameLabel.getFont();
    nameLabel.setFont(new Font(font.getName(), Font.BOLD, font.getSize()));
  add(titlePanel, new GridBagConstraints(0, 0, 1, 1, 1.0, 0.0, GridBagConstraints.CENTER, GridBagConstraints.BOTH,
  valuesPanel.setLayout(new GridBagLayout());
  add(valuesPanel, new GridBagConstraints(0, 1, 1, 1, 1.0, 0.0, GridBagConstraints.CENTER,
origin: libgdx/libgdx

public ValueDialog (JComponent component, String name, String description) {
  setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);
  setLayout(new GridBagLayout());
  setModal(true);
    ((JSpinner.DefaultEditor)((JSpinner)component).getEditor()).getTextField().setColumns(4);
  JPanel descriptionPanel = new JPanel();
  descriptionPanel.setLayout(new GridBagLayout());
  getContentPane().add(
    descriptionPanel,
    new GridBagConstraints(0, 0, 2, 1, 1.0, 0.0, GridBagConstraints.CENTER, GridBagConstraints.BOTH, new Insets(0, 0, 0,
      0), 0, 0));
  descriptionPanel.setBackground(Color.white);
    descriptionPanel.add(descriptionText, new GridBagConstraints(0, 0, 1, 1, 1.0, 0.0, GridBagConstraints.CENTER,
      GridBagConstraints.BOTH, new Insets(5, 5, 5, 5), 0, 0));
    descriptionText.setWrapStyleWord(true);
    descriptionText.setLineWrap(true);
  getContentPane().add(
    panel,
    new GridBagConstraints(0, 1, 1, 1, 1.0, 1.0, GridBagConstraints.CENTER, GridBagConstraints.NONE, new Insets(5, 5, 0,
      5), 0, 0));
  panel.add(new JLabel(name + ":"));
  getContentPane().add(
    JButton cancelButton = new JButton("Cancel");
origin: chewiebug/GCViewer

Panel logoPanel = new Panel();
ImageIcon logoIcon = ImageHelper.loadImageIcon(LocalisationHelper.getString("about_dialog_image"));
JLabel la_icon = new JLabel(logoIcon);
la_icon.setBorder(new SoftBevelBorder(SoftBevelBorder.LOWERED));
logoPanel.add(la_icon);
JPanel versionPanel = new JPanel();
versionPanel.setBorder(BorderFactory.createEmptyBorder(10, 10, 10, 10));
versionPanel.setLayout(new GridBagLayout());
JLabel copyright = new JLabel("\u00A9" + " 2011-2018: Joerg Wuethrich and contributors", JLabel.CENTER);
GridBagConstraints gridBagConstraints = new GridBagConstraints();
gridBagConstraints.anchor = GridBagConstraints.NORTH;
gridBagConstraints.fill = GridBagConstraints.VERTICAL;
Insets insetsGapOnTop = new Insets(10, 0, 0, 0);
GridBagConstraints gridBagConstraintsGapOnTop = new GridBagConstraints();
gridBagConstraintsGapOnTop.gridy = 1;
gridBagConstraintsGapOnTop.insets = insetsGapOnTop;
getContentPane().add("North", logoPanel);
getContentPane().add("Center", versionPanel);
getContentPane().add("South", buttonPanel);
pack();
setResizable(false);
origin: bonnyfone/vectalign

panelInput = new JPanel(new GridLayout(1, 2, 10, 0));
JPanel panelFrom = new JPanel(new BorderLayout());
JPanel panelTo = new JPanel(new BorderLayout());
svgFrom = new SVGDrawingPanel();
svgFrom.setPreferredSize(new Dimension(190, 200));
svgTo.setPreferredSize(new Dimension(190, 200));
btnEditFrom = new JButton("Edit Path");
btnEditTo = new JButton("Edit Path");
btnSvgFrom = new JButton("Load SVG");
panelStrokeSize.add(new JLabel("Stroke size: "), BorderLayout.WEST);
SpinnerModel spinnerModel =
    new SpinnerNumberModel(2, //initial value
btnMorphAnimation.setMargin(new Insets(0, 0, 0, 0));
panelOutput.setLayout(new GridBagLayout());
GridBagConstraints gc1 = new GridBagConstraints();
gc1.fill = GridBagConstraints.BOTH;
gc1.gridx = 0;
gc1.weightx = 0.975f;
gc1.weighty = 0.5f;
GridBagConstraints gcBtn1 = new GridBagConstraints();
gcBtn1.fill = GridBagConstraints.BOTH;
gcBtn1.gridx = 1;
origin: libgdx/libgdx

protected void initializeComponents () {
  setLayout(new GridBagLayout());
    titlePanel = new JPanel(new GridBagLayout());
    add(titlePanel, new GridBagConstraints(1, 0, 1, 1, 1.0, 0.0, GridBagConstraints.WEST, GridBagConstraints.HORIZONTAL,
      new Insets(3, 0, 3, 0), 0, 0));
    titlePanel.setCursor(Cursor.getPredefinedCursor(Cursor.HAND_CURSOR));
      descriptionLabel = new JLabel(description);
      titlePanel.add(descriptionLabel, new GridBagConstraints(1, 0, 1, 1, 1.0, 0.0, GridBagConstraints.WEST,
        GridBagConstraints.NONE, new Insets(3, 6, 3, 6), 0, 0));
      removeButton = new JButton("X");
      titlePanel.add(removeButton, new GridBagConstraints(4, 0, 1, 1, 0.0, 0.0, GridBagConstraints.CENTER,
        GridBagConstraints.NONE, new Insets(0, 0, 0, 6), 0, 0));
    contentPanel = new JPanel(new GridBagLayout());
    add(contentPanel, new GridBagConstraints(1, 1, 1, 1, 0.0, 0.0, GridBagConstraints.CENTER, GridBagConstraints.BOTH,
      new Insets(0, 6, 6, 6), 0, 0));
    advancedPanel = new JPanel(new GridBagLayout());
    add(advancedPanel, new GridBagConstraints(1, 2, 1, 1, 0.0, 0.0, GridBagConstraints.CENTER, GridBagConstraints.BOTH,
      new Insets(0, 6, 6, 6), 0, 0));
  removeButton.addActionListener(new ActionListener() {
    public void actionPerformed (ActionEvent event) {
      removePanel();
origin: libgdx/libgdx

getContentPane().add(splitPane, BorderLayout.CENTER);
    JPanel propertiesPanel = new JPanel(new GridBagLayout());
    rightSplit.add(propertiesPanel, JSplitPane.TOP);
    propertiesPanel.setBorder(new CompoundBorder(BorderFactory.createEmptyBorder(3, 0, 6, 6), BorderFactory
      .createTitledBorder("Editor Properties")));
        editorPropertiesPanel = new JPanel(new GridBagLayout());
        scroll.setViewportView(editorPropertiesPanel);
        scroll.getVerticalScrollBar().setUnitIncrement(70);
    rightSplit.add(rightSplitPane, JSplitPane.BOTTOM);
    JPanel propertiesPanel = new JPanel(new GridBagLayout());
        JPanel influencersPanel = new JPanel(new GridBagLayout());
        influencerBox = new JComboBox(new DefaultComboBoxModel());
        JButton addInfluencerButton = new JButton("Add");
        addInfluencerButton.addActionListener(new ActionListener() {
          @Override
          public void actionPerformed (ActionEvent e) {
    propertiesPanel = new JPanel(new GridBagLayout());
        controllerPropertiesPanel = new JPanel(new GridBagLayout());
        scroll.setViewportView(controllerPropertiesPanel);
        scroll.getVerticalScrollBar().setUnitIncrement(70);
origin: stackoverflow.com

 public static void main(String[] args) {
  JFrame frame = new JFrame();
  frame.setLayout(new GridBagLayout());
  JPanel panel = new JPanel();
  panel.add(new JLabel("This is a label"));
  panel.setBorder(new LineBorder(Color.BLACK)); // make it easy to see
  frame.add(panel, new GridBagConstraints());
  frame.setSize(400, 400);
  frame.setLocationRelativeTo(null);
  frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  frame.setVisible(true);
}
origin: libgdx/libgdx

private void uiLayout () {
  topPanel = new JPanel(new GridBagLayout());
  topPanel.setBorder(BorderFactory.createEmptyBorder(5, 5, 5, 5));
  warningNotice = new JLabel("List of third party extensions for LibGDX");
  warningNotice2 = new JLabel("These are not maintained by the LibGDX team, please see the support links for info and help");
  warningNotice.setHorizontalAlignment(JLabel.CENTER);
  warningNotice2.setHorizontalAlignment(JLabel.CENTER);
  topPanel.add(warningNotice, new GridBagConstraints(0, 0, 1, 1, 1, 0, NORTH, HORIZONTAL, new Insets(0, 0, 0, 0), 0, 0));
  topPanel.add(warningNotice2, new GridBagConstraints(0, 1, 1, 1, 1, 0, NORTH, HORIZONTAL, new Insets(0, 0, 0, 0), 0, 0));
  separator.setBackground(new Color(85, 85, 85));
  topPanel.add(separator, new GridBagConstraints(0, 2, 1, 1, 1, 1, NORTH, HORIZONTAL, new Insets(0, 0, 0, 0), 0, 0));
  bottomPanel = new JPanel(new GridBagLayout());
  buttonPanel = new JPanel(new GridBagLayout());
  buttonPanel.setBorder(BorderFactory.createEmptyBorder(5, 5, 5, 5));
  buttonOK = new SetupButton("Save");
origin: jMonkeyEngine/jmonkeyengine

GridBagConstraints gbc;
JPanel mainPanel = new JPanel(new GridBagLayout());
JButton ok = new JButton(resourceBundle.getString("button.ok"));               
JButton cancel = new JButton(resourceBundle.getString("button.cancel"));
icon = new JLabel(imageFile != null ? new ImageIcon(imageFile) : null);
gammaBox.setSelected(source.isGammaCorrection());
gbc = new GridBagConstraints();
gbc.weightx = 0.5;
gbc.gridx = 0;
mainPanel.add(vsyncBox, gbc);
gbc = new GridBagConstraints();
gbc.weightx = 0.5;
gbc.gridx = 3;
gbc.insets = new Insets(4, 4, 4, 4);
gbc.weightx = 0.5;
gbc.gridx = 0;
ok.addActionListener(new ActionListener() {
this.getContentPane().add(mainPanel);
origin: libgdx/libgdx

private void initializeComponents (String chartTitle, boolean hasIndependent) {
  JPanel contentPanel = getContentPanel();
    formPanel = new JPanel(new GridBagLayout());
    contentPanel.add(formPanel, new GridBagConstraints(5, 5, 1, 1, 0, 0, GridBagConstraints.EAST, GridBagConstraints.NONE,
      new Insets(0, 0, 0, 6), 0, 0));
      JLabel label = new JLabel("High:");
      formPanel.add(label, new GridBagConstraints(2, 1, 1, 1, 0, 0, GridBagConstraints.EAST, GridBagConstraints.NONE,
        new Insets(0, 0, 0, 6), 0, 0));
      formPanel.add(highMinSlider, new GridBagConstraints(3, 1, 1, 1, 0, 0, GridBagConstraints.WEST,
        GridBagConstraints.NONE, new Insets(0, 0, 0, 0), 0, 0));
      highRangeButton = new JButton("<");
      highRangeButton.setBorder(BorderFactory.createEmptyBorder(6, 6, 6, 6));
      formPanel.add(highRangeButton, new GridBagConstraints(5, 1, 1, 1, 0.0, 0, GridBagConstraints.WEST,
        GridBagConstraints.NONE, new Insets(0, 1, 0, 0), 0, 0));
      JLabel label = new JLabel("Low:");
      formPanel.add(label, new GridBagConstraints(2, 2, 1, 1, 0, 0, GridBagConstraints.EAST, GridBagConstraints.NONE,
        new Insets(0, 0, 0, 6), 0, 0));
      lowRangeButton = new JButton("<");
      lowRangeButton.setBorder(BorderFactory.createEmptyBorder(6, 6, 6, 6));
      formPanel.add(lowRangeButton, new GridBagConstraints(5, 2, 1, 1, 0.0, 0, GridBagConstraints.WEST,
origin: libgdx/libgdx

private void initializeComponents () {
  setLayout(new GridBagLayout());
    titlePanel = new JPanel(new GridBagLayout());
    add(titlePanel, new GridBagConstraints(1, 0, 1, 1, 1.0, 0.0, GridBagConstraints.WEST, GridBagConstraints.HORIZONTAL,
      new Insets(3, 0, 3, 0), 0, 0));
    titlePanel.setCursor(Cursor.getPredefinedCursor(Cursor.HAND_CURSOR));
      JLabel label = new JLabel(name);
      titlePanel.add(label, new GridBagConstraints(0, 0, 1, 1, 0.0, 0.0, GridBagConstraints.WEST, GridBagConstraints.NONE,
        new Insets(3, 6, 3, 6), 0, 0));
      label.setFont(label.getFont().deriveFont(Font.BOLD));
      descriptionLabel = new JLabel(description);
      titlePanel.add(descriptionLabel, new GridBagConstraints(1, 0, 1, 1, 1.0, 0.0, GridBagConstraints.WEST,
        GridBagConstraints.NONE, new Insets(3, 6, 3, 6), 0, 0));
    contentPanel = new JPanel(new GridBagLayout());
    add(contentPanel, new GridBagConstraints(1, 1, 1, 1, 0.0, 0.0, GridBagConstraints.CENTER, GridBagConstraints.BOTH,
      new Insets(0, 6, 6, 6), 0, 0));
    advancedPanel = new JPanel(new GridBagLayout());
    add(advancedPanel, new GridBagConstraints(1, 2, 1, 1, 0.0, 0.0, GridBagConstraints.CENTER, GridBagConstraints.BOTH,
      new Insets(0, 6, 6, 6), 0, 0));
origin: log4j/log4j

public LogFactor5ErrorDialog(JFrame jframe, String message) {
 super(jframe, "Error", true);
 JButton ok = new JButton("Ok");
 ok.addActionListener(new ActionListener() {
  public void actionPerformed(ActionEvent e) {
   hide();
  }
 });
 JPanel bottom = new JPanel();
 bottom.setLayout(new FlowLayout());
 bottom.add(ok);
 JPanel main = new JPanel();
 main.setLayout(new GridBagLayout());
 wrapStringOnPanel(message, main);
 getContentPane().add(main, BorderLayout.CENTER);
 getContentPane().add(bottom, BorderLayout.SOUTH);
 show();
}
//--------------------------------------------------------------------------
origin: stackoverflow.com

private JTextField nameField = new JTextField(10);
private JComboBox searchTermsCombo = new JComboBox();
private JButton addNewFieldBtn = new JButton("Add New Field");
private JButton submitBtn = new JButton("Submit");
private JPanel centerPanel = new JPanel(new GridBagLayout());
private int gridY = 0;
 centerPanel.add(new JLabel("Name:"), gbc);
 gbc = createGBC(1, gridY);
 centerPanel.add(nameField, gbc);     
 gridY++;
 centerPanel.add(new JLabel("Search Terms:"), gbc);
   win.pack();
   win.setLocationRelativeTo(null);
 GridBagConstraints gbc = new GridBagConstraints();
 gbc.gridx = x;
 gbc.gridy = y;
 gbc.anchor = (x == 0) ? gbc.LINE_START : gbc.LINE_END;
 gbc.fill = (x == 0) ? gbc.BOTH : gbc.HORIZONTAL;
 gbc.insets = (x == 0) ? new Insets(5, 0, 5, 5) : new Insets(5, 5, 5, 0);
 return gbc;
origin: cmusphinx/sphinx4

GridBagLayout gridBag = new GridBagLayout();
GridBagConstraints constraints;
Insets insets;
contentPane.setLayout(gridBag);
filename = new JTextField(12);
JLabel filenameLabel = new JLabel("Filename:");
filenameLabel.setLabelFor(filename);
insets = new Insets(12, 12, 0, 0);  // top, left, bottom, right
constraints = new GridBagConstraints(
    0, 0, 1, 1,                     // x, y, width, height
    0.0, 0.0,                       // weightx, weighty
gridBag.setConstraints(filenameLabel, constraints);
contentPane.add(filenameLabel);
insets = new Insets(12, 7, 0, 12);  // top, left, bottom, right
constraints = new GridBagConstraints(
    1, 0, 1, 1,                     // x, y, width, height
    1.0, 1.0,                       // weightx, weighty
gridBag.setConstraints(filename, constraints);
contentPane.add(filename);
gridBag.setConstraints(okButton, constraints);
contentPane.add(okButton);
origin: stackoverflow.com

  JFrame frame = new JFrame("Testing");
  frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  frame.setContentPane(new JLabel(new ImageIcon(img)));
  frame.setLayout(new GridBagLayout());
  GridBagConstraints gbc = new GridBagConstraints();
  gbc.gridwidth = GridBagConstraints.REMAINDER;
  frame.add(new JLabel("Hello world"), gbc);
  frame.add(new JLabel("I'm on top"), gbc);
  frame.add(new JButton("Clickity-clackity"), gbc);
  frame.pack();
  frame.setLocationRelativeTo(null);
  frame.setVisible(true);
} catch (IOException exp) {
  exp.printStackTrace();
java.awtGridBagLayout

Javadoc

The GridBagLayout class is a flexible layout manager that aligns components vertically and horizontally, without requiring that the components be of the same size. Each GridBagLayout object maintains a dynamic, rectangular grid of cells, with each component occupying one or more cells, called its display area.

Each component managed by a GridBagLayout is associated with an instance of GridBagConstraints. The constraints object specifies where a component's display area should be located on the grid and how the component should be positioned within its display area. In addition to its constraints object, the GridBagLayout also considers each component's minimum and preferred sizes in order to determine a component's size.

Grid coordinate (0,0) is in the upper left corner of the container with x increasing to the right and y increasing downward.

To use a grid bag layout effectively, you must customize one or more of the GridBagConstraints objects that are associated with its components. You customize a GridBagConstraints object by setting one or more of its instance variables:

GridBagConstraints#gridx, GridBagConstraints#gridySpecifies the cell at the upper left of the component's display area, where the cell at the origin of the grid has address gridx = 0, gridy = 0. Use GridBagConstraints.RELATIVE (the default value) to specify that the component be placed immediately following (along the x axis for gridx or the y axis for gridy) the component that was added to the container just before this component was added. GridBagConstraints#gridwidth, GridBagConstraints#gridheightSpecifies the number of cells in a row (for gridwidth) or column (for gridheight) in the component's display area. The default value is 1. Use GridBagConstraints.REMAINDER to specify that the component be the last one in its row (for gridwidth) or column (for gridheight). Use GridBagConstraints.RELATIVE to specify that the component be the next to last one in its row (for gridwidth) or column (for gridheight). GridBagConstraints#fillUsed when the component's display area is larger than the component's requested size to determine whether (and how) to resize the component. Possible values are GridBagConstraints.NONE (the default), GridBagConstraints.HORIZONTAL (make the component wide enough to fill its display area horizontally, but don't change its height), GridBagConstraints.VERTICAL (make the component tall enough to fill its display area vertically, but don't change its width), and GridBagConstraints.BOTH (make the component fill its display area entirely). GridBagConstraints#ipadx, GridBagConstraints#ipadySpecifies the component's internal padding within the layout, how much to add to the minimum size of the component. The width of the component will be at least its minimum width plus (ipadx * 2) pixels (since the padding applies to both sides of the component). Similarly, the height of the component will be at least the minimum height plus (ipady * 2) pixels. GridBagConstraints#insetsSpecifies the component's external padding, the minimum amount of space between the component and the edges of its display area. GridBagConstraints#anchorUsed when the component is smaller than its display area to determine where (within the display area) to place the component.

Valid values are:

  • GridBagConstraints.NORTH
  • GridBagConstraints.SOUTH
  • GridBagConstraints.WEST
  • GridBagConstraints.EAST
  • GridBagConstraints.NORTHWEST
  • GridBagConstraints.NORTHEAST
  • GridBagConstraints.SOUTHWEST
  • GridBagConstraints.SOUTHEAST
  • GridBagConstraints.CENTER (the default)
  • GridBagConstraints#weightx, GridBagConstraints#weightyUsed to determine how to distribute space, which is important for specifying resizing behavior. Unless you specify a weight for at least one component in a row (weightx) and column (weighty), all the components clump together in the center of their container. This is because when the weight is zero (the default), the GridBagLayout object puts any extra space between its grid of cells and the edges of the container.

    The following figure shows ten components (all buttons) managed by a grid bag layout:

    Each of the ten components has the fill field of its associated GridBagConstraints object set to GridBagConstraints.BOTH. In addition, the components have the following non-default constraints:

    • Button1, Button2, Button3: weightx = 1.0
    • Button4: weightx = 1.0, gridwidth = GridBagConstraints.REMAINDER
    • Button5: gridwidth = GridBagConstraints.REMAINDER
    • Button6: gridwidth = GridBagConstraints.RELATIVE
    • Button7: gridwidth = GridBagConstraints.REMAINDER
    • Button8: gridheight = 2, weighty = 1.0
    • Button9, Button 10: gridwidth = GridBagConstraints.REMAINDER

    Note: The following code example includes classes that do not appear in this specification. Their inclusion is purely to serve as a demonstration.

    Here is the code that implements the example shown above:


     
    import java.awt.*; 
    import java.util.*; 
    import java.applet.Applet; 
    public class GridBagEx1 extends Applet { 
    protected void makebutton(String name, 
    GridBagLayout gridbag, 
    GridBagConstraints c) { 
    Button button = new Button(name); 
    gridbag.setConstraints(button, c); 
    add(button); 
    } 
    public void init() { 
    GridBagLayout gridbag = new GridBagLayout(); 
    GridBagConstraints c = new GridBagConstraints(); 
    setFont(new Font("Helvetica", Font.PLAIN, 14)); 
    setLayout(gridbag); 
    c.fill = GridBagConstraints.BOTH; 
    c.weightx = 1.0; 
    makebutton("Button1", gridbag, c); 
    makebutton("Button2", gridbag, c); 
    makebutton("Button3", gridbag, c); 
    c.gridwidth = GridBagConstraints.REMAINDER; //end row 
    makebutton("Button4", gridbag, c); 
    c.weightx = 0.0;		   //reset to the default 
    makebutton("Button5", gridbag, c); //another row 
    c.gridwidth = GridBagConstraints.RELATIVE; //next-to-last in row 
    makebutton("Button6", gridbag, c); 
    c.gridwidth = GridBagConstraints.REMAINDER; //end row 
    makebutton("Button7", gridbag, c); 
    c.gridwidth = 1;	   	   //reset to the default 
    c.gridheight = 2; 
    c.weighty = 1.0; 
    makebutton("Button8", gridbag, c); 
    c.weighty = 0.0;		   //reset to the default 
    c.gridwidth = GridBagConstraints.REMAINDER; //end row 
    c.gridheight = 1;		   //reset to the default 
    makebutton("Button9", gridbag, c); 
    makebutton("Button10", gridbag, c); 
    setSize(300, 100); 
    } 
    public static void main(String args[]) { 
    Frame f = new Frame("GridBag Layout Example"); 
    GridBagEx1 ex1 = new GridBagEx1(); 
    ex1.init(); 
    f.add("Center", ex1); 
    f.pack(); 
    f.setSize(f.getPreferredSize()); 
    f.show(); 
    } 
    } 
    

    Most used methods

    • <init>
      Creates a grid bag layout manager.
    • setConstraints
      Sets the constraints for the specified component in this layout.
    • getConstraints
      Gets the constraints for the specified component. A copy of the actual GridBagConstraints object is
    • addLayoutComponent
      Adds the specified component with the specified name to the layout.
    • invalidateLayout
      Invalidates the layout, indicating that if the layout manager has cached information it should be di
    • preferredLayoutSize
      Determines the preferred size of the parent container using this grid bag layout. Most applications
    • layoutContainer
      Lays out the specified container using this grid bag layout. This method reshapes components in the
    • minimumLayoutSize
      Determines the minimum size of the parent container using this grid bag layout. Most applications d
    • removeLayoutComponent
      Removes the specified component from this layout. Most applications do not call this method directly
    • add
    • getLayoutDimensions
      Determines column widths and row heights for the layout grid. Most applications do not call this met
    • toString
      Returns a string representation of this grid bag layout's values.
    • getLayoutDimensions,
    • toString

    Popular in Java

    • Running tasks concurrently on multiple threads
    • scheduleAtFixedRate (ScheduledExecutorService)
    • addToBackStack (FragmentTransaction)
    • getSupportFragmentManager (FragmentActivity)
    • Menu (java.awt)
    • Calendar (java.util)
      Calendar is an abstract base class for converting between a Date object and a set of integer fields
    • Set (java.util)
      A Set is a data structure which does not allow duplicate elements.
    • Executors (java.util.concurrent)
      Factory and utility methods for Executor, ExecutorService, ScheduledExecutorService, ThreadFactory,
    • Filter (javax.servlet)
      A filter is an object that performs filtering tasks on either the request to a resource (a servlet o
    • JFileChooser (javax.swing)
    • Top 15 Vim Plugins
    Tabnine Logo
    • Products

      Search for Java codeSearch for JavaScript code
    • IDE Plugins

      IntelliJ IDEAWebStormVisual StudioAndroid StudioEclipseVisual Studio CodePyCharmSublime TextPhpStormVimAtomGoLandRubyMineEmacsJupyter NotebookJupyter LabRiderDataGripAppCode
    • Company

      About UsContact UsCareers
    • Resources

      FAQBlogTabnine AcademyStudentsTerms of usePrivacy policyJava Code IndexJavascript Code Index
    Get Tabnine for your IDE now