user14413339
user14413339

Reputation:

Java Swing How to use GridBagLayout and GridBagConstraints properly?

I am having trouble applying the GridBagLayout on my code. I have two version of the code, one is working and the other is not.

This one is not working:

public class Tutorial extends JFrame{

JButton button1, button2, button3, button4, button5;
JPanel panel;
GridBagConstraints grid;

public Tutorial() {
    super("Java Program");
    setVisible(true);
    setSize(500, 500);
    setLocation(new Point(500, 150));
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

    addComponents();
}

public void addComponents() {
    panel = new JPanel();
    grid = new GridBagConstraints();
    panel.setLayout(new GridBagLayout());

    button1 = new JButton();
    button1.setText("Button 1");
    grid.gridx = 0;
    grid.gridy = 0;
    panel.add(button1, grid);

    button2 = new JButton();
    button2.setText("Button 2");
    grid.gridx = 1;
    grid.gridy = 1;
    panel.add(button2, grid);

    button3 = new JButton();
    button3.setText("Button 3");
    grid.gridx = 2;
    grid.gridy = 2;
    panel.add(button3, grid);

    button4 = new JButton();
    button4.setText("Button 4");
    grid.gridx = 3;
    grid.gridy = 3;
    panel.add(button4, grid);

    button5 = new JButton();
    button5.setText("Button 5");
    grid.gridx = 4;
    grid.gridy = 4;
    panel.add(button5, grid);

    panel.add(button1);
    panel.add(button2);
    panel.add(button3);
    panel.add(button4);
    panel.add(button5);

    add(panel);
}

public static void main(String[] args) {

    Tutorial frame = new Tutorial();
    frame.setVisible(true);

}

}

To give a bit of context I got used to this kind of code structure while watching some tutorials. I also noticed that when I use NetBeans to make GUI just by drag and dropping components the code structure is almost the same as the one I am currently using so, I tried to stick with this coding structure. But the problem is the effect of GridBagLayout on my code is not working and I don't know where the problem is, I kinda need a bit of explanation.

So here I made a simple one without the class variable, constructor and method also this one is working:

public class TestClass {

public static void main(String[] args) {

    JFrame frame = new JFrame();
    JPanel panel = new JPanel();
    GridBagConstraints grid = new GridBagConstraints();

    JButton button1 = new JButton("Button 1");
    JButton button2 = new JButton("Button 2");
    JButton button3 = new JButton("Button 3");
    JButton button4 = new JButton("Button 4");
    JButton button5 = new JButton("Button 5");

    frame.setVisible(true);
    frame.setSize(500, 300);
    frame.setLocation(new Point(500, 150));
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

    panel.setLayout(new GridBagLayout());

    grid.gridx = 1;
    grid.gridy = 1;
    panel.add(button1, grid);

    grid.gridx = 2;
    grid.gridy = 2;
    panel.add(button2, grid);

    grid.gridx = 3;
    grid.gridy = 3;
    panel.add(button3, grid);

    grid.gridx = 4;
    grid.gridy = 4;
    panel.add(button4, grid);

    grid.gridx = 5;
    grid.gridy = 5;
    panel.add(button5, grid);

    frame.add(panel);

}

}

So basically both of them are the same, its just the structure that is different.

Upvotes: 0

Views: 66

Answers (1)

Abra
Abra

Reputation: 20914

Note that method setVisible should be called after you add all the components.

In the code that doesn't work, you are making a few mistakes.

  • You are adding the buttons twice. Once with the GridBagConstraints and once without. You only need to add components once.
  • You are calling method setVisible twice. Once in the constructor and once in method main. You only need to call method setVisible once.

I got used to this kind of code structure while watching some tutorials

I recommend Oracle's tutorial: Creating a GUI With JFC/Swing.

when I use NetBeans to make GUI just by drag and dropping components

If you really want to learn Swing programming, I recommend not using a GUI builder.

Upvotes: 1

Related Questions