First name: <text field>
Last name: <text field>
But I can't figure out how to align the items so they will be in straight lines (like above).
I'm trying this:
super(new BorderLayout( ));
// Create panel 1
JPanel panel1 = new JPanel();
// New label and text field PANELS & added to the main panel
JPanel labelPanel1 = new JPanel(new GridLayout(2, 1));
JPanel fieldPanel1 = new JPanel(new GridLayout(2, 1));
panel1.add(labelPanel1, BorderLayout.WEST);
panel1.add(fieldPanel1, BorderLayout.CENTER);
// New label and text field
JTextField fieldlabel1 = new JTextField(20);
JLabel fname = new JLabel("First name:", JLabel.RIGHT);
// Set label for text field
fname.setLabelFor(fieldlabel1);
// Add label and text field to their panels
labelPanel1.add(fname);
JPanel p = new JPanel(new FlowLayout(FlowLayout.LEFT));
p.add(fieldlabel1);
fieldPanel1.add(p);
It gives me a nice:
First name: <text field>
if I change the numbers in new GridLayout(2, 1)); to (1, 1) and then, I'm trying to add the next label and text field, like this:
JTextField fieldlabel2 = new JTextField(6);
JLabel lname = new JLabel("Last name:", JLabel.RIGHT);
lname.setLabelFor(fieldlabel2);
labelPanel1.add(lname);
JPanel r = new JPanel(new FlowLayout(FlowLayout.LEFT));
r.add(fieldlabel2);
fieldPanel1.add®;
and change GridLayout to GridLayout(2, 1));
The text fields are aligned nicely one under the other, but the labels are NOT. Labels are moved close to each other.
What am I doing wrong? Please help
Edited by onlybarca, 27 November 2010 - 08:46 AM.