Question

The code below was copied from Oracle tutorials and modified to place components OTHER than buttons--a label and two text fields and an additional button. It produces the form that it should (shown below), so you shouldn't have to scrutinize the first 30 or so lines of code:

enter image description here

If I UNcomment out the two lines that refer to "box" and comment out the adjoining lines that refer to button, look at where the checkbox is placed.

enter image description here

I can't figure out how what's going on. I want the checkbox on the last line, as the code SEEMS to say it MUST be positioned, yet it is NOT.

All I am doing is changing TWO LINES that refer to button to refer instead to checkbox. Are the rules for GridBagLayout component-dependent?? What should I do (and why isn't the code working as is) to get checkbox on bottom line?

public class Testy {   
  public static void addComponentsToPane(Container pane) {
      pane.setLayout(new GridBagLayout());
      GridBagConstraints c = new GridBagConstraints();
      c.fill = GridBagConstraints.HORIZONTAL; 
      c.weightx = 0.5;
      c.gridx = 0;      c.gridy = 0;
      JLabel label = new JLabel("Button 1 ");
      pane.add(label, c);
      JTextField text = new JTextField("Button 2 ");
      c.gridx = 1;      c.gridy = 0;
      pane.add(text, c);
      JButton button = new JButton("Button 3 ");
      c.gridx = 2;      c.gridy = 0;
      pane.add(button, c);
      text = new JTextField("LONG Text 4 ");
      c.ipady = 40;      
      c.weightx = 0.0;
      c.gridwidth = 3;
      c.gridx = 0;      c.gridy = 1;
      pane.add(text, c);
      button = new JButton("5 ");
      c.ipady = 0;       
      c.weighty = 1.0;   
      c.gridx = 1;      c.gridy = 2;    
      c.gridwidth = 2;   
      pane.add(button, c);
// Here's the part of the code in question
      button = new JButton("666");  // This placement is FINE, at bottom of form.
  //  JCheckBox box = new JCheckBox("Me?");   // "box" does NOT get placed on last line.
      c.weighty = 0;     
      c.gridx = 0;     c.gridy = 3;    
      c.gridwidth = 1;   
  //  pane.add(box);
      pane.add(button, c);
    }

Here are the required imports and two methods that I removed so the problem code area above could be seen without scrolling:

import java.awt.*;
import javax.swing.JButton;
import javax.swing.JCheckBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JTextField;

  private static void createAndShowGUI() /* calls addComponentsToPane() */{
        JFrame frame = new JFrame("GridBagLayoutDemo");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);      
        addComponentsToPane(frame.getContentPane());
        frame.pack();
        frame.setVisible(true);
    }
    public static void main(String[] args) /* calls createAndShowGUI() */ {
        javax.swing.SwingUtilities.invokeLater(new Runnable() {
            public void run() {
                createAndShowGUI();
            }        });    }
}
Was it helpful?

Solution

//  pane.add(box);
    pane.add(button, c);

You just have to use the right method:

pane.add(box, c);

See Container#add(Component comp) and Container#add(Component comp, Object constraints).

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top