Skip to Main Content

Java SE (Java Platform, Standard Edition)

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

align JLabels and JTextFields vertically in different areas

836771Apr 21 2011 — edited May 17 2011
I like to have 3 TitledBorders for 3 different areas of my frame.
Each area has its own components - JLabels, JTextField, etc.
How to align JLabels and JTextFields vertically in different areas?
e.g. for the following test program, how to configure label1, label2, label3 so that their right sides all align vertically and
tf1, tf2, tf3 so that their left sides all align vertically?
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.GridLayout;
import java.awt.Insets;

import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.border.TitledBorder;

public class TitledBorderDemo extends JFrame {
  public TitledBorderDemo() {
    super("TitledBorderDemo");

    JTextField tf1 = new JTextField("hello", 6);
    JTextField tf2 = new JTextField("hello", 12);
    JTextField tf3 = new JTextField("test");
    JTextField tf4 = new JTextField("test2");

    JLabel label1 = new JLabel("1234567890ertyuiyup label");
    JLabel label2 = new JLabel("zzzzzzzzzzzzzzzzzzzzzzzzzzzzzz long label");
    JLabel label3 = new JLabel("short label");
    JLabel label4 = new JLabel("test");

    JPanel panel_tf = new JPanel(new GridBagLayout());
    JPanel panel_pf = new JPanel(new GridBagLayout());
    JPanel panel_ftf = new JPanel(new GridBagLayout());
    
    GridBagConstraints constraints = new GridBagConstraints(0, 0, 3, 3,
            0.0, 0.0, GridBagConstraints.WEST, GridBagConstraints.NONE,
            new Insets(10, 10, 10, 10), 0, 0);

    constraints.gridx = 0;
    constraints.gridy = 0;
    constraints.gridwidth = 2;
    constraints.anchor = GridBagConstraints.WEST;
    panel_tf.add(label1, constraints);
    
    constraints.gridx = 2;
    constraints.gridwidth = 1;
    constraints.anchor = GridBagConstraints.EAST;
    panel_tf.add(tf1, constraints);

    constraints.gridx = 0;
    constraints.gridy = 1;
    constraints.gridwidth = 2;
    constraints.anchor = GridBagConstraints.WEST;
    panel_pf.add(label2, constraints);

    constraints.gridx = 2;
    constraints.gridwidth = 1;
    constraints.anchor = GridBagConstraints.EAST;
    panel_pf.add(tf2, constraints);
    
    constraints.gridx = 0;
    constraints.gridy = 2;
    constraints.gridwidth = 2;
    constraints.anchor = GridBagConstraints.WEST;
    panel_ftf.add(label3, constraints);

    constraints.gridx = 2;
    constraints.gridwidth = 1;
    constraints.anchor = GridBagConstraints.EAST;
    panel_ftf.add(tf3, constraints);

    constraints.gridx = 3;
    constraints.gridwidth = 1;
    constraints.anchor = GridBagConstraints.WEST;
    panel_ftf.add(label4, constraints);

    constraints.gridx = 4;
    constraints.anchor = GridBagConstraints.EAST;
    panel_ftf.add(tf4, constraints);


    panel_tf.setBorder(new TitledBorder("JTextField1"));
    panel_pf.setBorder(new TitledBorder("JTextField2"));
    panel_ftf.setBorder(new TitledBorder("JTextField3"));

    JPanel pan = new JPanel(new GridLayout(3, 1, 10, 10));
    pan.add(panel_tf);
    pan.add(panel_pf);
    pan.add(panel_ftf);
    this.add(pan);
  }

  public static void main(String args[]) {
    JFrame frame = new TitledBorderDemo();
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.setSize(600, 450);
    frame.setVisible(true);
  }
}
This post has been answered by splungebob on Apr 21 2011
Jump to Answer
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on Jun 14 2011
Added on Apr 21 2011
19 comments
2,135 views