Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.java.programmer > #3263

Re: align Swing JLabels and JTextFields vertically with

From albert kao <albertkao3@gmail.com>
Newsgroups comp.lang.java.programmer
Subject Re: align Swing JLabels and JTextFields vertically with
Date 2011-04-26 07:02 -0700
Organization http://groups.google.com
Message-ID <6bf11e93-e5ec-47b7-b2e8-bcecde2d391f@e8g2000vbz.googlegroups.com> (permalink)
References <af65db34-f2a6-474b-9a33-6a4c4b8f902f@a19g2000prj.googlegroups.com> <1S0sp.9618$__1.1031@newsfe03.iad> <Nb1sp.13785$vC5.10856@newsfe01.iad> <hm3sp.33733$Ay5.30278@newsfe07.iad>

Show all headers | View raw


On Apr 21, 7:47 pm, Knute Johnson <nos...@knutejohnson.com> 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?

Back to comp.lang.java.programmer | Previous | NextPrevious in thread | Next in thread | Find similar


Thread

align Swing JLabels and JTextFields vertically with albert kao <albertkao3@gmail.com> - 2011-04-21 09:49 -0700
  Re: align Swing JLabels and JTextFields vertically with Knute Johnson <nospam@knutejohnson.com> - 2011-04-21 13:56 -0700
    Re: align Swing JLabels and JTextFields vertically with Knute Johnson <nospam@knutejohnson.com> - 2011-04-21 14:19 -0700
      Re: align Swing JLabels and JTextFields vertically with Knute Johnson <nospam@knutejohnson.com> - 2011-04-21 16:47 -0700
        Re: align Swing JLabels and JTextFields vertically with albert kao <albertkao3@gmail.com> - 2011-04-26 07:02 -0700
          Re: align Swing JLabels and JTextFields vertically with Knute Johnson <nospam@knutejohnson.com> - 2011-04-26 10:23 -0700
  Re: align Swing JLabels and JTextFields vertically with Roedy Green <see_website@mindprod.com.invalid> - 2011-04-22 12:22 -0700

csiph-web