align Swing JLabels and JTextFields vertically with

Discussion in 'Java' started by albert kao, Apr 21, 2011.

  1. albert kao

    albert kao Guest

    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 Swing 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?
    My jdk version is 6.


    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);
    }
    }
     
    albert kao, Apr 21, 2011
    #1
    1. Advertising

  2. On 04/21/2011 09:49 AM, albert kao wrote:
    > 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 Swing 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?
    > My jdk version is 6.
    >
    >
    > 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);
    > }
    > }


    I posted a simple solution to this a few weeks ago. I don't remember if
    it was you who posted or not but nothing was heard out of the original
    poster.

    The solution basically to not use multiple JPanels but to put all of the
    fields in one panel, add some extra padding where you want the borders
    and then to put the border around the bounds of the components.

    I'll see if I can find it again.

    --

    Knute Johnson
    s/knute/nospam/
     
    Knute Johnson, Apr 21, 2011
    #2
    1. Advertising

  3. On 04/21/2011 01:56 PM, Knute Johnson wrote:

    > I posted a simple solution to this a few weeks ago. I don't remember if
    > it was you who posted or not but nothing was heard out of the original
    > poster.
    >
    > The solution basically to not use multiple JPanels but to put all of the
    > fields in one panel, add some extra padding where you want the borders
    > and then to put the border around the bounds of the components.
    >
    > I'll see if I can find it again.
    >


    I found it. It won't work quite as you have specified as it only allows
    one border per JComponent. I've got a couple of ideas though, I'll get
    back to you.

    import java.awt.*;
    import javax.swing.*;
    import javax.swing.border.*;

    public class test extends JPanel {
    JLabel l1,l2;
    JTextField tf1,tf2;

    public test() {
    super(new GridBagLayout());

    GridBagConstraints c = new GridBagConstraints();
    c.gridy = 0; c.insets = new Insets(2,2,2,2);
    c.fill = GridBagConstraints.HORIZONTAL;

    add(new JLabel("Label 000"),c);
    add(new JTextField("Field 0"),c);

    ++c.gridy;
    c.insets = new Insets(20,2,2,2);
    l1 = new JLabel("Label 1");
    add(l1,c);
    tf1 = new JTextField("Field 00001");
    add(tf1,c);

    ++c.gridy;
    c.insets = new Insets(2,2,2,2);
    l2 = new JLabel("Label 2");
    add(l2,c);
    tf2 = new JTextField("Field 2");
    add(tf2,c);

    setBorder(new MyTitledBorder(
    BorderFactory.createLineBorder(Color.BLACK,2),"Title"));
    }

    class MyTitledBorder extends TitledBorder {
    Rectangle rv;

    public MyTitledBorder(Border border, String title) {
    super(border,title);
    }

    public void paintBorder(Component c, Graphics g, int x, int y,
    int width, int height) {
    rv = l1.getBounds();
    rv.add(tf1.getBounds());
    rv.add(l2.getBounds());
    rv.add(tf2.getBounds());
    rv.grow(10,10);

    super.paintBorder(c,g,(int)rv.getX(),(int)rv.getY()-10,
    (int)rv.getWidth(),(int)rv.getHeight()+10);
    }
    }

    public static void main(String[] args) {
    EventQueue.invokeLater(new Runnable() {
    public void run() {
    JFrame f = new JFrame();
    f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    test t = new test();
    f.add(t,BorderLayout.CENTER);
    f.setSize(320,240);
    f.setVisible(true);
    }
    });
    }
    }

    --

    Knute Johnson
    s/knute/nospam/
     
    Knute Johnson, Apr 21, 2011
    #3
  4. How about this? One thing I don't understand is how SpringLayout
    determines sizes.

    import java.awt.*;
    import javax.swing.*;
    import javax.swing.border.*;

    public class test extends JPanel {
    JLabel l1,l2,l3;
    JTextField tf1,tf2,tf3;

    public test() {
    super(new GridBagLayout());

    GridBagConstraints c = new GridBagConstraints();
    c.fill = GridBagConstraints.BOTH;
    c.weightx = c.weighty = 1.0;
    c.gridx = 0;

    SpringLayout spring = new SpringLayout();

    JPanel p1 = new JPanel(spring);
    p1.setBorder(BorderFactory.createTitledBorder("Panel 1"));
    add(p1,c);

    l1 = new JLabel("Label1");
    spring.putConstraint(SpringLayout.EAST,l1,-2,
    SpringLayout.HORIZONTAL_CENTER,p1);
    spring.putConstraint(SpringLayout.VERTICAL_CENTER,l1,0,
    SpringLayout.VERTICAL_CENTER,p1);
    tf1 = new JTextField("this is text field 1",20);
    spring.putConstraint(SpringLayout.WEST,tf1,2,
    SpringLayout.HORIZONTAL_CENTER,p1);
    spring.putConstraint(SpringLayout.VERTICAL_CENTER,tf1,0,
    SpringLayout.VERTICAL_CENTER,p1);

    p1.add(l1);
    p1.add(tf1);

    JPanel p2 = new JPanel(spring);
    p2.setBorder(BorderFactory.createTitledBorder("Panel 3"));
    add(p2,c);

    l2 = new JLabel("************ LONG LABEL ***********");
    spring.putConstraint(SpringLayout.EAST,l2,-2,
    SpringLayout.HORIZONTAL_CENTER,p2);
    spring.putConstraint(SpringLayout.VERTICAL_CENTER,l2,0,
    SpringLayout.VERTICAL_CENTER,p2);
    tf2 = new JTextField("Short Textfield");
    spring.putConstraint(SpringLayout.WEST,tf2,2,
    SpringLayout.HORIZONTAL_CENTER,p2);
    spring.putConstraint(SpringLayout.VERTICAL_CENTER,tf2,0,
    SpringLayout.VERTICAL_CENTER,p2);

    p2.add(l2);
    p2.add(tf2);

    JPanel p3 = new JPanel(spring);
    p3.setBorder(BorderFactory.createTitledBorder("Panel 3"));
    add(p3,c);

    l3 = new JLabel("Label 3");
    spring.putConstraint(SpringLayout.EAST,l3,-2,
    SpringLayout.HORIZONTAL_CENTER,p3);
    spring.putConstraint(SpringLayout.VERTICAL_CENTER,l3,0,
    SpringLayout.VERTICAL_CENTER,p3);

    tf3 = new JTextField("TextField 3",30);
    spring.putConstraint(SpringLayout.WEST,tf3,2,
    SpringLayout.HORIZONTAL_CENTER,p3);
    spring.putConstraint(SpringLayout.VERTICAL_CENTER,tf3,0,
    SpringLayout.VERTICAL_CENTER,p3);

    p3.add(l3);
    p3.add(tf3);

    System.out.println(spring.maximumLayoutSize(p3));
    System.out.println(spring.minimumLayoutSize(p3));
    System.out.println(spring.preferredLayoutSize(p3));
    }

    public static void main(String[] args) {
    EventQueue.invokeLater(new Runnable() {
    public void run() {
    JFrame f = new JFrame();
    f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    test t = new test();
    f.add(t,BorderLayout.CENTER);
    f.setSize(600,450);
    f.setVisible(true);
    }
    });
    }
    }

    --

    Knute Johnson
    s/knute/nospam/
     
    Knute Johnson, Apr 22, 2011
    #4
  5. albert kao

    Roedy Green Guest

    On Thu, 21 Apr 2011 09:49:01 -0700 (PDT), albert kao
    <> wrote, quoted or indirectly quoted someone who
    said :

    >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?


    see http://mindprod.com/jgloss/gridbaglayout.html
    --
    Roedy Green Canadian Mind Products
    http://mindprod.com
    Politicians complain that Kindles and iBooks are killing jobs by
    destroying the paper book industry. I see it that they have create a way
    to produce books for less than a third the cost without destroying forests
    and emitting greenhouse gases in the process. They have created wealth.
    They are encouraging literacy and cutting the costs of education.
     
    Roedy Green, Apr 22, 2011
    #5
  6. albert kao

    albert kao Guest

    On Apr 21, 7:47 pm, Knute Johnson <> wrote:
    > How about this?  One thing I don't understand is how SpringLayout
    > determines sizes.
    >
    > import java.awt.*;
    > import javax.swing.*;
    > import javax.swing.border.*;
    >
    > public class test extends JPanel {
    >      JLabel l1,l2,l3;
    >      JTextField tf1,tf2,tf3;
    >
    >      public test() {
    >          super(new GridBagLayout());
    >
    >          GridBagConstraints c = new GridBagConstraints();
    >          c.fill = GridBagConstraints.BOTH;
    >          c.weightx = c.weighty = 1.0;
    >          c.gridx = 0;
    >
    >          SpringLayout spring = new SpringLayout();
    >
    >          JPanel p1 = new JPanel(spring);
    >          p1.setBorder(BorderFactory.createTitledBorder("Panel 1"));
    >          add(p1,c);
    >
    >          l1 = new JLabel("Label1");
    >          spring.putConstraint(SpringLayout.EAST,l1,-2,
    >           SpringLayout.HORIZONTAL_CENTER,p1);
    >          spring.putConstraint(SpringLayout.VERTICAL_CENTER,l1,0,
    >           SpringLayout.VERTICAL_CENTER,p1);
    >          tf1 = new JTextField("this is text field 1",20);
    >          spring.putConstraint(SpringLayout.WEST,tf1,2,
    >           SpringLayout.HORIZONTAL_CENTER,p1);
    >          spring.putConstraint(SpringLayout.VERTICAL_CENTER,tf1,0,
    >           SpringLayout.VERTICAL_CENTER,p1);
    >
    >          p1.add(l1);
    >          p1.add(tf1);
    >
    >          JPanel p2 = new JPanel(spring);
    >          p2.setBorder(BorderFactory.createTitledBorder("Panel 3"));
    >          add(p2,c);
    >
    >          l2 = new JLabel("************ LONG LABEL ***********");
    >          spring.putConstraint(SpringLayout.EAST,l2,-2,
    >           SpringLayout.HORIZONTAL_CENTER,p2);
    >          spring.putConstraint(SpringLayout.VERTICAL_CENTER,l2,0,
    >           SpringLayout.VERTICAL_CENTER,p2);
    >          tf2 = new JTextField("Short Textfield");
    >          spring.putConstraint(SpringLayout.WEST,tf2,2,
    >           SpringLayout.HORIZONTAL_CENTER,p2);
    >          spring.putConstraint(SpringLayout.VERTICAL_CENTER,tf2,0,
    >           SpringLayout.VERTICAL_CENTER,p2);
    >
    >          p2.add(l2);
    >          p2.add(tf2);
    >
    >          JPanel p3 = new JPanel(spring);
    >          p3.setBorder(BorderFactory.createTitledBorder("Panel 3"));
    >          add(p3,c);
    >
    >          l3 = new JLabel("Label 3");
    >          spring.putConstraint(SpringLayout.EAST,l3,-2,
    >           SpringLayout.HORIZONTAL_CENTER,p3);
    >          spring.putConstraint(SpringLayout.VERTICAL_CENTER,l3,0,
    >           SpringLayout.VERTICAL_CENTER,p3);
    >
    >          tf3 = new JTextField("TextField 3",30);
    >          spring.putConstraint(SpringLayout.WEST,tf3,2,
    >           SpringLayout.HORIZONTAL_CENTER,p3);
    >          spring.putConstraint(SpringLayout.VERTICAL_CENTER,tf3,0,
    >           SpringLayout.VERTICAL_CENTER,p3);
    >
    >          p3.add(l3);
    >          p3.add(tf3);
    >
    > System.out.println(spring.maximumLayoutSize(p3));
    > System.out.println(spring.minimumLayoutSize(p3));
    > System.out.println(spring.preferredLayoutSize(p3));
    >      }
    >
    >      public static void main(String[] args) {
    >          EventQueue.invokeLater(new Runnable() {
    >              public void run() {
    >                  JFrame f = new JFrame();
    >                  f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    >                  test t = new test();
    >                  f.add(t,BorderLayout.CENTER);
    >                  f.setSize(600,450);
    >                  f.setVisible(true);
    >              }
    >          });
    >      }
    >
    > }
    >
    > --
    >
    > Knute Johnson
    > s/knute/nospam/


    Your program works using SpringLayout.
    Thank you!
    How to use GridBagLayout to do the same thing?
     
    albert kao, Apr 26, 2011
    #6
  7. On 04/26/2011 07:02 AM, albert kao wrote:
    > Your program works using SpringLayout.
    > Thank you!
    > How to use GridBagLayout to do the same thing?


    You can't. That's why I did it with SpringLayout.

    --

    Knute Johnson
    s/knute/nospam/
     
    Knute Johnson, Apr 26, 2011
    #7
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. homer

    div Wont Vertically Align

    homer, Jan 26, 2005, in forum: HTML
    Replies:
    6
    Views:
    4,641
    homer
    Jan 28, 2005
  2. Guybrush Threepwood
    Replies:
    1
    Views:
    468
    Mitja Trampus
    Dec 14, 2005
  3. Replies:
    1
    Views:
    491
  4. Trevor

    CSS Vertically Align Text

    Trevor, Apr 28, 2006, in forum: HTML
    Replies:
    5
    Views:
    9,452
    Barbara de Zoete
    Apr 29, 2006
  5. Olivier Scalbert

    How to align swing buttons vertically ?

    Olivier Scalbert, Nov 13, 2011, in forum: Java
    Replies:
    13
    Views:
    4,793
    Knute Johnson
    Nov 14, 2011
Loading...

Share This Page