Question

I'm trying to work with GridBagLayout inside a JPanel, on a JLayeredPane; I have two columns and I'd like to increase the bounds of the second in order to keep it from clipping.

Here's my code:

    JLayeredPane lPane = new JLayeredPane();
    lPane.setBounds(0, 0, 1200, 700);
    frame.add(lPane, BorderLayout.CENTER);

    JPanel left = new JPanel(new GridBagLayout());
    left.setBounds(0, 0, 175, 700);
    left.setBackground(Color.BLACK);

    GridBagConstraints leftGrid = new GridBagConstraints();
    JLabel label;

    label = new JLabel("Player's Goods:");
    leftGrid.fill = GridBagConstraints.NONE;
    leftGrid.anchor = GridBagConstraints.NORTHWEST;
    leftGrid.insets = new Insets(10,10,10,10);
    leftGrid.weighty = 0.5;
    leftGrid.gridx = 0;
    leftGrid.gridy = 0;
    left.add(label, leftGrid);

    label = new JLabel("");
    leftGrid.weighty = 0;
    leftGrid.weightx = 0;
    leftGrid.gridx = 0;
    leftGrid.gridy = 1;
    left.add(label, leftGrid);

    label = new JLabel("Nails:");
    leftGrid.ipady = 50;
    leftGrid.gridx = 0;
    leftGrid.gridy = 2;
    left.add(label, leftGrid);

    label = new JLabel("Wheat:");
    leftGrid.gridx = 0;
    leftGrid.gridy = 3;
    left.add(label, leftGrid);

    label = new JLabel("Armor:");
    leftGrid.gridx = 0;
    leftGrid.gridy = 4;
    left.add(label, leftGrid);

    label = new JLabel("Gold:");
    leftGrid.gridx = 0;
    leftGrid.gridy = 5;
    left.add(label, leftGrid);

    label = new JLabel("Weapons:");
    leftGrid.gridx = 0;
    leftGrid.gridy = 6;
    left.add(label, leftGrid);

    label = new JLabel("Spice:");
    leftGrid.gridx = 0;
    leftGrid.gridy = 7;
    left.add(label, leftGrid);

    label = new JLabel("0 lbs.");
    label.setHorizontalTextPosition(JLabel.LINE_START);
    leftGrid.fill = GridBagConstraints.HORIZONTAL;
    leftGrid.anchor = GridBagConstraints.WEST;
    leftGrid.ipady = 0;
    leftGrid.ipadx = 50;
    leftGrid.weightx = 1;
    leftGrid.gridx = 1;
    leftGrid.gridy = 2;
    left.add(label, leftGrid);

    label = new JLabel("0 lbs.");
    leftGrid.gridx = 1;
    leftGrid.gridy = 3;
    left.add(label, leftGrid);

    label = new JLabel("0 lbs.");
    leftGrid.gridx = 1;
    leftGrid.gridy = 4;
    left.add(label, leftGrid);

    label = new JLabel("0 lbs.");
    leftGrid.gridx = 1;
    leftGrid.gridy = 5;
    left.add(label, leftGrid);

    label = new JLabel("9999 lbs.");
    leftGrid.gridx = 1;
    leftGrid.gridy = 6;
    left.add(label, leftGrid);

    label = new JLabel("0 lbs.");
    leftGrid.gridx = 1;
    leftGrid.gridy = 7;
    left.add(label, leftGrid);

    label = new JLabel("666 Coins");
    leftGrid.ipady = 50;
    leftGrid.weighty = 1;
    leftGrid.gridx = 1;
    leftGrid.gridy = 8;
    left.add(label, leftGrid);

    lPane.add(left, new Integer(0), 0);

To specify: almost all of the "0 lbs." JLabels (999 lbs. added to give an idea of possible width) seem to be right-aligned. How do I make them left-aligned, and how do I then adjust the space between the first column and the second?

Was it helpful?

Solution

There are a combination of things at play...

label = new JLabel("Player's Goods:");
leftGrid.fill = GridBagConstraints.NONE;
leftGrid.anchor = GridBagConstraints.NORTHWEST;
leftGrid.insets = new Insets(10, 10, 10, 10);
leftGrid.weighty = 0.5;
leftGrid.gridx = 0;
leftGrid.gridy = 0;
add(label, leftGrid);

Is going to affect the overall width of the left column, it may be better to include

leftGrid.gridwidth = GridBagConstraints.REMAINDER;

Which will allow it to expand the remaining columns, just make sure you reset it to leftGrid.gridwidth = 1; before you use it again.

leftGrid.insets = new Insets(10, 10, 10, 10);

Is going to add 20 pixels between the left and right columns...

This...

label.setHorizontalTextPosition(JLabel.LINE_START);

Affects the position of the text relative to the icon, not it's general alignment within the label (and JLabel.LINE_START doesn't actually exist)

Instead, I think you want...

label.setHorizontalAlignment(JLabel.LEFT);

This...

leftGrid.ipadx = 50

Will increase the space between the columns by another 25 pixels (up to 45 including the insets)

Be Careful with the constraints, they can have compounding effects.

I was able to produce...

Layout

Using...

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.EventQueue;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;
import javax.swing.border.LineBorder;

public class TestGridBagLayout100 {

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

    public TestGridBagLayout100() {
        EventQueue.invokeLater(new Runnable() {
            @Override
            public void run() {
                try {
                    UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
                } catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
                }

                JFrame frame = new JFrame("Testing");
                frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                frame.setLayout(new BorderLayout());
                frame.add(new TestPane());
                frame.pack();
                frame.setLocationRelativeTo(null);
                frame.setVisible(true);
            }
        });
    }

    public class TestPane extends JPanel {

        public TestPane() {
            setLayout(new GridBagLayout());
//            setBackground(Color.BLACK);

            GridBagConstraints leftGrid = new GridBagConstraints();
            JLabel label;

            label = new JLabel("Player's Goods:");
            leftGrid.fill = GridBagConstraints.NONE;
            leftGrid.anchor = GridBagConstraints.NORTHWEST;
//            leftGrid.insets = new Insets(10, 10, 10, 10);
            leftGrid.weighty = 0.5;
            leftGrid.gridx = 0;
            leftGrid.gridy = 0;
            leftGrid.gridwidth = GridBagConstraints.REMAINDER;
            add(label, leftGrid);

            //??
            leftGrid.gridwidth = 1;
            label = new JLabel("");
            leftGrid.weighty = 0;
            leftGrid.weightx = 0;
            leftGrid.gridx = 0;
            leftGrid.gridy = 1;
            add(label, leftGrid);

            label = new JLabel("Nails:");
            leftGrid.gridy = 0;
            leftGrid.ipady = 50;
            leftGrid.gridx = 0;
            leftGrid.gridy = 2;
            add(label, leftGrid);

            label = new JLabel("Wheat:");
            leftGrid.gridx = 0;
            leftGrid.gridy = 3;
            add(label, leftGrid);

            label = new JLabel("Armor:");
            leftGrid.gridx = 0;
            leftGrid.gridy = 4;
            add(label, leftGrid);

            label = new JLabel("Gold:");
            leftGrid.gridx = 0;
            leftGrid.gridy = 5;
            add(label, leftGrid);

            label = new JLabel("Weapons:");
            leftGrid.gridx = 0;
            leftGrid.gridy = 6;
            add(label, leftGrid);

            label = new JLabel("Spice:");
            leftGrid.gridx = 0;
            leftGrid.gridy = 7;
            add(label, leftGrid);

            label = new JLabel("0 lbs.");
            label.setBorder(new LineBorder(Color.RED));
            label.setHorizontalAlignment(JLabel.LEFT);
            leftGrid.fill = GridBagConstraints.HORIZONTAL;
            leftGrid.anchor = GridBagConstraints.WEST;
            leftGrid.ipady = 0;
//            leftGrid.ipadx = 50;
            leftGrid.weightx = 1;
            leftGrid.gridx = 1;
            leftGrid.gridy = 2;
            add(label, leftGrid);

            label = new JLabel("0 lbs.");
            leftGrid.gridx = 1;
            leftGrid.gridy = 3;
            add(label, leftGrid);

            label = new JLabel("0 lbs.");
            leftGrid.gridx = 1;
            leftGrid.gridy = 4;
            add(label, leftGrid);

            label = new JLabel("0 lbs.");
            leftGrid.gridx = 1;
            leftGrid.gridy = 5;
            add(label, leftGrid);

            label = new JLabel("9999 lbs.");
            leftGrid.gridx = 1;
            leftGrid.gridy = 6;
            add(label, leftGrid);

            label = new JLabel("0 lbs.");
            leftGrid.gridx = 1;
            leftGrid.gridy = 7;
            add(label, leftGrid);

            label = new JLabel("666 Coins");
//            leftGrid.ipady = 50;
            leftGrid.weighty = 1;
            leftGrid.gridx = 1;
            leftGrid.gridy = 8;
            add(label, leftGrid);
        }
    }

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