简体   繁体   中英

How to align JLabel next to a JTextField in a GridBagLayout

My form is very simple I just want to have labels next to text fields. The labels are default centering and it makes the form look weird. I want to the labels exactly next to the textfield. I have played with the horizontal alignment of the labels and textfields but it did not change anything.

Here is my code:

JPanel root = new JPanel(new GridBagLayout());
                 GridBagConstraints gbc = new GridBagConstraints();
                 gbc.insets= new Insets(0,0,0,0);

                 newVehicleRecord.setLayout(new BorderLayout());
                 newVehicleRecord.add(root,BorderLayout.PAGE_START);


                 JLabel title = new JLabel("New Vehicle Record - Customer ID:" + customerIDInfo.getText());
                 title.setFont(fontTitle);
                 gbc.weightx = 0;
                 gbc.gridx = 0; gbc.gridy = 0; gbc.gridwidth= 2;

                 root.add(title,gbc);

                 gbc.gridx = 0; gbc.gridy = 0; gbc.gridwidth= 1;
                 root.add(Box.createVerticalStrut(15),gbc);


                 gbc.gridx = 0; gbc.gridy = 2;
                 JLabel classificationLabel = new JLabel("Classification:");
                 classificationLabel.setHorizontalAlignment(JLabel.RIGHT);
                 root.add(classificationLabel,gbc);

                 gbc.gridx = 1; gbc.gridy = 2;
                 JTextField classificationTextField = new JTextField(10);
                 classificationTextField.setHorizontalAlignment(JTextField.LEFT);
                 root.add(classificationTextField,gbc);

                 gbc.gridx = 0; gbc.gridy = 3;
                  JLabel modelLabel = new JLabel("Model:");
                 root.add(modelLabel,gbc);

                 gbc.gridx = 1; gbc.gridy = 3;
                 JTextField modelTextField = new JTextField(10);
                 root.add(modelTextField,gbc);

                 gbc.gridx = 0; gbc.gridy = 4;
                  JLabel makeLabel = new JLabel("Make:");
                 root.add(makeLabel,gbc);

                 gbc.gridx = 1; gbc.gridy = 4;
                 JTextField makeTextField = new JTextField(10);
                 root.add(makeTextField,gbc);

I get the following display: http://prntscr.com/6j3iki

As you can see there is a lot of empty space between the label and the textfield which I don't want.

I want to the labels exactly next to the textfield.

You need to play with the anchor constraint:

gbc.anchor = GridBagConstraints.LINE_END;
panel.add(label, gbc);
gbc.anchor = GridBagConstraints.LINE_START;
panel.add(textField, gbc);

Also you would probably want something like:

gbc.insets = new Insets(5, 10, 5, 10);

so the right edge of the label has some space between the left edge of the text field.

Read the section from the Swing tutorial on How to Use GridBagLayout for more information on all the constraints.

Something like this:

             gbc.gridx = 0; gbc.gridy = 3;
             gbc.anchor = GridBagConstraints.EAST;
             JLabel modelLabel = new JLabel("Model:");
             root.add(modelLabel,gbc);

So you need to add the line

gbc.anchor = GridBagConstraints.EAST;

to each your label.

Another possibility:

             gbc.gridx = 0; gbc.gridy = 3;
             gbc.fill = GridBagConstraints.HORIZONTAL;
             JLabel modelLabel = new JLabel("Model:");
             modelLabel.setHorizontalAlignment(SwingConstants.RIGHT);
             root.add(modelLabel,gbc);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM