0

I'm currently using Java Swing and GridBagLayout to try and organize some buttons. I'm running into an issue where my "cookieClick" and "shopButton" buttons are taking up two spaces (gridx 0 & 1) when firstly, I set their grid x to 1, and set the weightx to 1.0. Any help would be great, thank you.

Swing Setup Class

package com.jetbrains;
import javax.swing.*;
import java.awt.*;

public class GUI {
    final int screenX = 1280, screenY = 720;

    JFrame mainGui = new JFrame();

    JPanel gamePanel = new JPanel();
    JPanel shopPanel = new JPanel();

    JButton userName = new JButton("a"); // this is the area for the user's username they input in
    JButton cookieCounter = new JButton("b"); // this is the counter for how many cookies the user currently has
    JButton cookieClick = new JButton("c"); // this is the label for the cookie we click on
    JButton shopButton = new JButton("d"); // this takes us to the shop, probably some if statement and set visible etc.

    GridBagConstraints gameLayout = new GridBagConstraints();

    public void guiConfig() {
        mainGui.setSize(screenX, screenY);
        mainGui.setTitle("journey");
        mainGui.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        mainGui.setResizable(false);
        mainGui.setBackground(Color.WHITE);
        mainGui.add(gamePanel);

        gamePanel.setSize(screenX, screenY);
        gamePanel.setLayout(new GridBagLayout());
    }

    public void labelPositioning() {
        gameLayout.fill = GridBagConstraints.HORIZONTAL;
        gameLayout.gridx = 0;
        gameLayout.gridwidth = 2;
        gameLayout.gridy = 0;
        gameLayout.weightx = 2.0;
        gameLayout.weighty = 1.0;
        gamePanel.add(cookieCounter, gameLayout);


        gameLayout.fill = GridBagConstraints.HORIZONTAL;
        gameLayout.gridx = 2;
        gameLayout.gridwidth = 1;
        gameLayout.gridy = 0;
        gameLayout.weightx = 1.0;
        gameLayout.weighty = 1.0;
        gamePanel.add(userName, gameLayout);


        gameLayout.fill = GridBagConstraints.HORIZONTAL;
        gameLayout.gridx = 1;
        gameLayout.gridwidth = 1;
        gameLayout.gridy = 1;
        gameLayout.weightx = 1.0;
        gameLayout.weighty = 1.0;
        gamePanel.add(cookieClick, gameLayout);


        gameLayout.fill = GridBagConstraints.HORIZONTAL;
        gameLayout.gridx = 1;
        gameLayout.gridwidth = 1;
        gameLayout.gridy = 2;
        gameLayout.weightx = 1.0;
        gameLayout.weighty = 1.0;
        gamePanel.add(shopButton, gameLayout);
    }

    public void frameVisibility() {
        mainGui.setVisible(true);
    }
}

Main Class

package com.jetbrains;
import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        GUI run = new GUI();
        run.guiConfig();
        run.labelPositioning();
        run.frameVisibility();
    }
}

1 Answers1

1

GridBagLayout is quite sophisticated and can sometimes do things you're not expecting. In this case, GridBagLayout is collapsing the first column, as there is (technically) nothing in it, this makes it "look" like your other buttons are also filling across two columns, when they are not.

For example. I added a empty JLabel to the first column (on the next row) and was able to produce the following output

Expanded columns

import java.awt.Color;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;

public class Test {

    public static void main(String[] args) {
        new Test();
    }

    public Test() {
        SwingUtilities.invokeLater(new Runnable() {
            @Override
            public void run() {
                GUI gui = new GUI();
                gui.guiConfig();
                gui.labelPositioning();
                gui.frameVisibility();
            }
        });
    }

    public class GUI {

        final int screenX = 1280, screenY = 720;

        JFrame mainGui = new JFrame();

        JPanel gamePanel = new JPanel();
        JPanel shopPanel = new JPanel();

        JButton userName = new JButton("userName"); // this is the area for the user's username they input in
        JButton cookieCounter = new JButton("cookieCounter"); // this is the counter for how many cookies the user currently has
        JButton cookieClick = new JButton("cookieClick"); // this is the label for the cookie we click on
        JButton shopButton = new JButton("shopButton"); // this takes us to the shop, probably some if statement and set visible etc.

        GridBagConstraints gameLayout = new GridBagConstraints();

        public void guiConfig() {
            mainGui.setSize(screenX, screenY);
            mainGui.setTitle("journey");
            mainGui.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
//            mainGui.setResizable(false);
            mainGui.setBackground(Color.WHITE);
            mainGui.add(gamePanel);

//            gamePanel.setSize(screenX, screenY);
            gamePanel.setLayout(new GridBagLayout());
        }

        public void labelPositioning() {
            gameLayout.fill = GridBagConstraints.HORIZONTAL;
            gameLayout.gridx = 0;
            gameLayout.gridwidth = 2;
            gameLayout.gridy = 0;
            gameLayout.weightx = 2.0;
            gameLayout.weighty = 1.0;
            gamePanel.add(cookieCounter, gameLayout);

            gameLayout.fill = GridBagConstraints.HORIZONTAL;
            gameLayout.gridx = 0;
            gameLayout.gridwidth = 1;
            gameLayout.gridy = 0;
            gameLayout.weightx = 1.0;
            gameLayout.weighty = 1.0;
            gamePanel.add(new JLabel(), gameLayout);

            gameLayout.fill = GridBagConstraints.HORIZONTAL;
            gameLayout.gridx = 2;
            gameLayout.gridwidth = 1;
            gameLayout.gridy = 0;
            gameLayout.weightx = 1.0;
            gameLayout.weighty = 1.0;
            gamePanel.add(userName, gameLayout);

            gameLayout.fill = GridBagConstraints.HORIZONTAL;
            gameLayout.gridx = 1;
            gameLayout.gridwidth = 1;
            gameLayout.gridy = 1;
            gameLayout.weightx = 1.0;
            gameLayout.weighty = 1.0;
            gamePanel.add(cookieClick, gameLayout);

            gameLayout.fill = GridBagConstraints.HORIZONTAL;
            gameLayout.gridx = 1;
            gameLayout.gridwidth = 1;
            gameLayout.gridy = 2;
            gameLayout.weightx = 1.0;
            gameLayout.weighty = 1.0;
            gamePanel.add(shopButton, gameLayout);
        }

        public void frameVisibility() {
            mainGui.setVisible(true);
        }
    }
}

You need to remember that the layout manager is using the preferred/min/max size of the components to make determinations about the width and height of the cells (as well as the other constraints) and this will affect how some rows/columns are sized

MadProgrammer
  • 343,457
  • 22
  • 230
  • 366
  • Wow, great information to know. Just curious, did you find this out through experience or through learned through some sort of source (text, video series, etc.). Thanks so much!! –  Dec 26 '19 at 06:04
  • Update: Just touched up my code with empty JLabels and I've got to say, you're remarkable. THANKS!! –  Dec 26 '19 at 06:12
  • @태양님 Most of this is from observation and some through crawling through the source code for `GridBagLayout` – MadProgrammer Dec 26 '19 at 06:14
  • 1
    I see, I'm trying my hardest on Java in my current high school year and people who can offer this awesome kind of advice like you deserve some kind of award. I'm not sure if there is some kind of award system on here (kind of like Reddit) but if there is then consider it a thank you/merry Christmas gift. –  Dec 26 '19 at 06:17