A
albert kao
How to align JLabel & JTextField which are outside a TitledBorder with
the JLabel & JTextField which are inside a TitledBorder?
My simplified Swing test program is as follows.
I use GridBagLayout for more powerful control.
i.e. how to align lastNameLabel with cityLabel & streetNumberLabel,
lastNameText with cityText & streetNumberText?
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.border.LineBorder;
import javax.swing.border.TitledBorder;
public class SwingAlignQuestion {
public static void main(String args[]) {
JPanel namePanel;
JPanel addressPanel;
JLabel lastNameLabel;
JTextField lastNameText;
JLabel cityLabel;
JTextField cityText;
JLabel streetNumberLabel;
JTextField streetNumberText;
TitledBorder addressBorder;
JFrame frame = new JFrame("Alignment Example");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setLayout(new GridBagLayout());
namePanel = new JPanel(new GridBagLayout());
addressPanel = new JPanel(new GridBagLayout());
addressBorder = new
TitledBorder(LineBorder.createGrayLineBorder(), "");
addressPanel.setBorder(addressBorder);
lastNameLabel = new JLabel();
lastNameText = new JTextField();
cityLabel = new JLabel();
cityText = new JTextField();
streetNumberLabel = new JLabel();
streetNumberText = new JTextField();
lastNameLabel.setText("lastName");
cityLabel.setText("co");
streetNumberLabel.setText("streetNumber");
Insets insets1 = new Insets(10, 10, 0, 0);
Insets insets2 = new Insets(10, 10, 0, 0);
GridBagConstraints constraints = new GridBagConstraints(
0, 12,
1, 1,
0.0, 0.0,
GridBagConstraints.WEST,
GridBagConstraints.NONE,
insets1,
0, 0);
constraints.gridx = 0;
constraints.gridy = 0;
constraints.anchor = GridBagConstraints.EAST;
constraints.insets = insets1;
namePanel.add(lastNameLabel, constraints);
constraints.gridx = 1;
constraints.anchor = GridBagConstraints.WEST;
constraints.insets = insets2;
namePanel.add(lastNameText, constraints);
constraints.gridx = 0;
constraints.gridy = 0;
constraints.anchor = GridBagConstraints.EAST;
constraints.fill = GridBagConstraints.NONE;
constraints.insets = insets1;
addressPanel.add(cityLabel, constraints);
constraints.gridx = 1;
constraints.anchor = GridBagConstraints.WEST;
constraints.insets = insets2;
addressPanel.add(cityText, constraints);
constraints.gridx = 0;
constraints.gridy = 1;
constraints.anchor = GridBagConstraints.EAST;
constraints.fill = GridBagConstraints.NONE;
constraints.insets = insets1;
addressPanel.add(streetNumberLabel, constraints);
constraints.gridx = 1;
constraints.anchor = GridBagConstraints.WEST;
constraints.insets = insets2;
addressPanel.add(streetNumberText, constraints);
constraints.gridx = 0;
constraints.gridy = 0;
constraints.anchor = GridBagConstraints.WEST;
constraints.insets = insets1;
frame.add(namePanel, constraints);
constraints.gridx = 0;
constraints.gridy = 1;
constraints.insets = insets1;
frame.add(addressPanel, constraints);
frame.setSize(423, 171);
frame.setVisible(true);
}
}
the JLabel & JTextField which are inside a TitledBorder?
My simplified Swing test program is as follows.
I use GridBagLayout for more powerful control.
i.e. how to align lastNameLabel with cityLabel & streetNumberLabel,
lastNameText with cityText & streetNumberText?
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.border.LineBorder;
import javax.swing.border.TitledBorder;
public class SwingAlignQuestion {
public static void main(String args[]) {
JPanel namePanel;
JPanel addressPanel;
JLabel lastNameLabel;
JTextField lastNameText;
JLabel cityLabel;
JTextField cityText;
JLabel streetNumberLabel;
JTextField streetNumberText;
TitledBorder addressBorder;
JFrame frame = new JFrame("Alignment Example");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setLayout(new GridBagLayout());
namePanel = new JPanel(new GridBagLayout());
addressPanel = new JPanel(new GridBagLayout());
addressBorder = new
TitledBorder(LineBorder.createGrayLineBorder(), "");
addressPanel.setBorder(addressBorder);
lastNameLabel = new JLabel();
lastNameText = new JTextField();
cityLabel = new JLabel();
cityText = new JTextField();
streetNumberLabel = new JLabel();
streetNumberText = new JTextField();
lastNameLabel.setText("lastName");
cityLabel.setText("co");
streetNumberLabel.setText("streetNumber");
Insets insets1 = new Insets(10, 10, 0, 0);
Insets insets2 = new Insets(10, 10, 0, 0);
GridBagConstraints constraints = new GridBagConstraints(
0, 12,
1, 1,
0.0, 0.0,
GridBagConstraints.WEST,
GridBagConstraints.NONE,
insets1,
0, 0);
constraints.gridx = 0;
constraints.gridy = 0;
constraints.anchor = GridBagConstraints.EAST;
constraints.insets = insets1;
namePanel.add(lastNameLabel, constraints);
constraints.gridx = 1;
constraints.anchor = GridBagConstraints.WEST;
constraints.insets = insets2;
namePanel.add(lastNameText, constraints);
constraints.gridx = 0;
constraints.gridy = 0;
constraints.anchor = GridBagConstraints.EAST;
constraints.fill = GridBagConstraints.NONE;
constraints.insets = insets1;
addressPanel.add(cityLabel, constraints);
constraints.gridx = 1;
constraints.anchor = GridBagConstraints.WEST;
constraints.insets = insets2;
addressPanel.add(cityText, constraints);
constraints.gridx = 0;
constraints.gridy = 1;
constraints.anchor = GridBagConstraints.EAST;
constraints.fill = GridBagConstraints.NONE;
constraints.insets = insets1;
addressPanel.add(streetNumberLabel, constraints);
constraints.gridx = 1;
constraints.anchor = GridBagConstraints.WEST;
constraints.insets = insets2;
addressPanel.add(streetNumberText, constraints);
constraints.gridx = 0;
constraints.gridy = 0;
constraints.anchor = GridBagConstraints.WEST;
constraints.insets = insets1;
frame.add(namePanel, constraints);
constraints.gridx = 0;
constraints.gridy = 1;
constraints.insets = insets1;
frame.add(addressPanel, constraints);
frame.setSize(423, 171);
frame.setVisible(true);
}
}