Path: csiph.com!x330-a1.tempe.blueboxinc.net!newsfeed.hal-mli.net!feeder1.hal-mli.net!news.glorb.com!postnews.google.com!e8g2000vbz.googlegroups.com!not-for-mail From: albert kao Newsgroups: comp.lang.java.programmer Subject: Re: align Swing JLabels and JTextFields vertically with Date: Tue, 26 Apr 2011 07:02:53 -0700 (PDT) Organization: http://groups.google.com Lines: 116 Message-ID: <6bf11e93-e5ec-47b7-b2e8-bcecde2d391f@e8g2000vbz.googlegroups.com> References: <1S0sp.9618$__1.1031@newsfe03.iad> NNTP-Posting-Host: 198.103.184.76 Mime-Version: 1.0 Content-Type: text/plain; charset=ISO-8859-1 Content-Transfer-Encoding: quoted-printable X-Trace: posting.google.com 1303826573 28868 127.0.0.1 (26 Apr 2011 14:02:53 GMT) X-Complaints-To: groups-abuse@google.com NNTP-Posting-Date: Tue, 26 Apr 2011 14:02:53 +0000 (UTC) Complaints-To: groups-abuse@google.com Injection-Info: e8g2000vbz.googlegroups.com; posting-host=198.103.184.76; posting-account=Qp4wBAoAAAAKeqMjnJXQBpmK6ZPaDDDy User-Agent: G2/1.0 X-HTTP-UserAgent: Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 5.1; Trident/4.0; .NET CLR 2.0.50727; .NET CLR 3.0.04506.648; .NET CLR 3.5.21022),gzip(gfe) Xref: x330-a1.tempe.blueboxinc.net comp.lang.java.programmer:3263 On Apr 21, 7:47=A0pm, Knute Johnson wrote: > How about this? =A0One 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 { > =A0 =A0 =A0JLabel l1,l2,l3; > =A0 =A0 =A0JTextField tf1,tf2,tf3; > > =A0 =A0 =A0public test() { > =A0 =A0 =A0 =A0 =A0super(new GridBagLayout()); > > =A0 =A0 =A0 =A0 =A0GridBagConstraints c =3D new GridBagConstraints(); > =A0 =A0 =A0 =A0 =A0c.fill =3D GridBagConstraints.BOTH; > =A0 =A0 =A0 =A0 =A0c.weightx =3D c.weighty =3D 1.0; > =A0 =A0 =A0 =A0 =A0c.gridx =3D 0; > > =A0 =A0 =A0 =A0 =A0SpringLayout spring =3D new SpringLayout(); > > =A0 =A0 =A0 =A0 =A0JPanel p1 =3D new JPanel(spring); > =A0 =A0 =A0 =A0 =A0p1.setBorder(BorderFactory.createTitledBorder("Panel 1= ")); > =A0 =A0 =A0 =A0 =A0add(p1,c); > > =A0 =A0 =A0 =A0 =A0l1 =3D new JLabel("Label1"); > =A0 =A0 =A0 =A0 =A0spring.putConstraint(SpringLayout.EAST,l1,-2, > =A0 =A0 =A0 =A0 =A0 SpringLayout.HORIZONTAL_CENTER,p1); > =A0 =A0 =A0 =A0 =A0spring.putConstraint(SpringLayout.VERTICAL_CENTER,l1,0= , > =A0 =A0 =A0 =A0 =A0 SpringLayout.VERTICAL_CENTER,p1); > =A0 =A0 =A0 =A0 =A0tf1 =3D new JTextField("this is text field 1",20); > =A0 =A0 =A0 =A0 =A0spring.putConstraint(SpringLayout.WEST,tf1,2, > =A0 =A0 =A0 =A0 =A0 SpringLayout.HORIZONTAL_CENTER,p1); > =A0 =A0 =A0 =A0 =A0spring.putConstraint(SpringLayout.VERTICAL_CENTER,tf1,= 0, > =A0 =A0 =A0 =A0 =A0 SpringLayout.VERTICAL_CENTER,p1); > > =A0 =A0 =A0 =A0 =A0p1.add(l1); > =A0 =A0 =A0 =A0 =A0p1.add(tf1); > > =A0 =A0 =A0 =A0 =A0JPanel p2 =3D new JPanel(spring); > =A0 =A0 =A0 =A0 =A0p2.setBorder(BorderFactory.createTitledBorder("Panel 3= ")); > =A0 =A0 =A0 =A0 =A0add(p2,c); > > =A0 =A0 =A0 =A0 =A0l2 =3D new JLabel("************ LONG LABEL ***********= "); > =A0 =A0 =A0 =A0 =A0spring.putConstraint(SpringLayout.EAST,l2,-2, > =A0 =A0 =A0 =A0 =A0 SpringLayout.HORIZONTAL_CENTER,p2); > =A0 =A0 =A0 =A0 =A0spring.putConstraint(SpringLayout.VERTICAL_CENTER,l2,0= , > =A0 =A0 =A0 =A0 =A0 SpringLayout.VERTICAL_CENTER,p2); > =A0 =A0 =A0 =A0 =A0tf2 =3D new JTextField("Short Textfield"); > =A0 =A0 =A0 =A0 =A0spring.putConstraint(SpringLayout.WEST,tf2,2, > =A0 =A0 =A0 =A0 =A0 SpringLayout.HORIZONTAL_CENTER,p2); > =A0 =A0 =A0 =A0 =A0spring.putConstraint(SpringLayout.VERTICAL_CENTER,tf2,= 0, > =A0 =A0 =A0 =A0 =A0 SpringLayout.VERTICAL_CENTER,p2); > > =A0 =A0 =A0 =A0 =A0p2.add(l2); > =A0 =A0 =A0 =A0 =A0p2.add(tf2); > > =A0 =A0 =A0 =A0 =A0JPanel p3 =3D new JPanel(spring); > =A0 =A0 =A0 =A0 =A0p3.setBorder(BorderFactory.createTitledBorder("Panel 3= ")); > =A0 =A0 =A0 =A0 =A0add(p3,c); > > =A0 =A0 =A0 =A0 =A0l3 =3D new JLabel("Label 3"); > =A0 =A0 =A0 =A0 =A0spring.putConstraint(SpringLayout.EAST,l3,-2, > =A0 =A0 =A0 =A0 =A0 SpringLayout.HORIZONTAL_CENTER,p3); > =A0 =A0 =A0 =A0 =A0spring.putConstraint(SpringLayout.VERTICAL_CENTER,l3,0= , > =A0 =A0 =A0 =A0 =A0 SpringLayout.VERTICAL_CENTER,p3); > > =A0 =A0 =A0 =A0 =A0tf3 =3D new JTextField("TextField 3",30); > =A0 =A0 =A0 =A0 =A0spring.putConstraint(SpringLayout.WEST,tf3,2, > =A0 =A0 =A0 =A0 =A0 SpringLayout.HORIZONTAL_CENTER,p3); > =A0 =A0 =A0 =A0 =A0spring.putConstraint(SpringLayout.VERTICAL_CENTER,tf3,= 0, > =A0 =A0 =A0 =A0 =A0 SpringLayout.VERTICAL_CENTER,p3); > > =A0 =A0 =A0 =A0 =A0p3.add(l3); > =A0 =A0 =A0 =A0 =A0p3.add(tf3); > > System.out.println(spring.maximumLayoutSize(p3)); > System.out.println(spring.minimumLayoutSize(p3)); > System.out.println(spring.preferredLayoutSize(p3)); > =A0 =A0 =A0} > > =A0 =A0 =A0public static void main(String[] args) { > =A0 =A0 =A0 =A0 =A0EventQueue.invokeLater(new Runnable() { > =A0 =A0 =A0 =A0 =A0 =A0 =A0public void run() { > =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0JFrame f =3D new JFrame(); > =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0f.setDefaultCloseOperation(JFrame.EXIT= _ON_CLOSE); > =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0test t =3D new test(); > =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0f.add(t,BorderLayout.CENTER); > =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0f.setSize(600,450); > =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0f.setVisible(true); > =A0 =A0 =A0 =A0 =A0 =A0 =A0} > =A0 =A0 =A0 =A0 =A0}); > =A0 =A0 =A0} > > } > > -- > > Knute Johnson > s/knute/nospam/ Your program works using SpringLayout. Thank you! How to use GridBagLayout to do the same thing?