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


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

Re: align Swing JLabels and JTextFields vertically with

Path csiph.com!x330-a1.tempe.blueboxinc.net!feeder1.hal-mli.net!nx01.iad01.newshosting.com!newshosting.com!69.16.185.16.MISMATCH!npeer02.iad.highwinds-media.com!news.highwinds-media.com!feed-me.highwinds-media.com!post01.iad.highwinds-media.com!newsfe07.iad.POSTED!6b61fb94!not-for-mail
From Knute Johnson <nospam@knutejohnson.com>
User-Agent Mozilla/5.0 (X11; U; Linux i686; en-US; rv:1.9.2.14) Gecko/20110223 Thunderbird/3.1.8
MIME-Version 1.0
Newsgroups comp.lang.java.programmer
Subject Re: align Swing JLabels and JTextFields vertically with
References <af65db34-f2a6-474b-9a33-6a4c4b8f902f@a19g2000prj.googlegroups.com> <1S0sp.9618$__1.1031@newsfe03.iad> <Nb1sp.13785$vC5.10856@newsfe01.iad>
In-Reply-To <Nb1sp.13785$vC5.10856@newsfe01.iad>
Content-Type text/plain; charset=ISO-8859-1; format=flowed
Content-Transfer-Encoding 7bit
Lines 99
Message-ID <hm3sp.33733$Ay5.30278@newsfe07.iad> (permalink)
X-Complaints-To abuse@newsdemon.com
NNTP-Posting-Date Thu, 21 Apr 2011 23:47:25 UTC
Organization NewsDemon
Date Thu, 21 Apr 2011 16:47:25 -0700
Xref x330-a1.tempe.blueboxinc.net comp.lang.java.programmer:3196

Show key headers only | View raw


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/

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