Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.java.programmer > #3193
| From | Knute Johnson <nospam@knutejohnson.com> |
|---|---|
| Newsgroups | comp.lang.java.programmer |
| Subject | Re: align Swing JLabels and JTextFields vertically with |
| References | <af65db34-f2a6-474b-9a33-6a4c4b8f902f@a19g2000prj.googlegroups.com> |
| Message-ID | <1S0sp.9618$__1.1031@newsfe03.iad> (permalink) |
| Organization | NewsDemon |
| Date | 2011-04-21 13:56 -0700 |
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/
Back to comp.lang.java.programmer | Previous | Next — Previous in thread | Next in thread | Find similar
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