align Swing JLabels and JTextFields vertically with

A

albert kao

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);
}
}
 
K

Knute Johnson

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.
 
K

Knute Johnson

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);
}
});
}
}
 
K

Knute Johnson

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);
}
});
}
}
 
R

Roedy Green

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.
 
A

albert kao

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);
             }
         });
     }

}

Your program works using SpringLayout.
Thank you!
How to use GridBagLayout to do the same thing?
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,579
Members
45,053
Latest member
BrodieSola

Latest Threads

Top