Debugging GridBagLayout - components clumped to center

582 views Asked by At

I'm trying to get the following layout

Label..................NumericField
Label2................NumericField
Label3333..........NumericField

Basically the (.) dots would be empty space. I had tried GridBagLayout with making the label's gridwidth as 5 and the NumericField's gridwidth as 1. I'm posting the code below. But I don't see the desired result and I see all components aligned at the center instead of Labels being at left border and NFs being at right border.

For Labels:

    GridBagConstraints localC = new GridBagConstraints();
    localC.anchor  = GridBagConstraints.FIRST_LINE_START;
    //localC.fill = GridBagConstraints.HORIZONTAL;
    localC.weightx = 1.0;
    localC.weighty = 1.0;
    localC.gridx    = 0;
    localC.gridy    = 0;
    localC.gridheight = 1;
    localC.gridwidth  = 5;
    localC.insets = new Insets(0, 0, 0, 0);

For NumericFields

    localC.anchor = GridBagConstraints.RELATIVE;
    localC.weightx = 0.5;
    localC.weighty = 0.5;
    localC.gridx = 1;
    localC.gridy = 0;
    localC.gridheight = 1;
    localC.gridwidth  = 1;

I'm new to JAVA and struggling with layouts generally.

1

There are 1 answers

0
MadProgrammer On

Add a value to the Insets right property, which will add that number of pixels to the right side of the column. You could also use GridBagConstraints#anchor set to GridBagConstraints.WEST, which will force the components in the columns to be positioned on the left hand side of the "column", this ensures that when a component in the column is wider, they won't be laid out in the middle of the resulting space.

gridwidth determines how a given cell will span across multiple columns, but if there are no other components in the resulting columns, they are discard (defaulted to 0), so in your layout, it's meaningless.

See How to Use GridBagLayout more details

GridBagLayout

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.JTextField;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;

public class Test {

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

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

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

    public class TestPane extends JPanel {

        public TestPane() {

            setLayout(new GridBagLayout());

            GridBagConstraints gbc = new GridBagConstraints();
            gbc.gridx = 0;
            gbc.gridy = 0;
            gbc.anchor = GridBagConstraints.WEST;
            gbc.insets = new Insets(0, 0, 0, 12);

            add(new JLabel("Label"), gbc);
            gbc.gridy++;
            add(new JLabel("Label2"), gbc);
            gbc.gridy++;
            add(new JLabel("Label3333"), gbc);

            gbc.gridx = 1;
            gbc.gridy = 0;
            gbc.insets = new Insets(0, 0, 0, 0);

            add(new JTextField(10), gbc);
            gbc.gridy++;
            add(new JTextField(10), gbc);
            gbc.gridy++;
            add(new JTextField(10), gbc);
        }

    }

}