Jump to content


Check out our Community Blogs

Register and join over 40,000 other developers!


Recent Status Updates

View All Updates

Photo
- - - - -

How to align items in a JAVA form using Grid Layout?

align grid

  • Please log in to reply
3 replies to this topic

#1 onlybarca

onlybarca

    CC Lurker

  • Just Joined
  • Pip
  • 2 posts

Posted 27 November 2010 - 05:46 AM

I'm trying to create a simple form:

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.

  • 0

#2 wim DC

wim DC

    Roar

  • Expert Member
  • PipPipPipPipPipPipPipPip
  • 2681 posts
  • Programming Language:Java, JavaScript, PL/SQL
  • Learning:Python

Posted 27 November 2010 - 07:44 AM

Perhaps something like:
        JPanel centerPanel = new JPanel(new GridLayout(0, 2,5,2));
        JLabel firstNameLabel = new JLabel("First name:", JLabel.RIGHT);
        JLabel lastNameLabel = new JLabel("First name:", JLabel.RIGHT);
        JTextField firstNameField = new JTextField(20);
        JTextField lastNameField = new JTextField(20);

        centerPanel.add(firstNameLabel);
        centerPanel.add(firstNameField);
        centerPanel.add(lastNameLabel);
        centerPanel.add(lastNameField);

        panel1.add(centerPanel);

By the way, you did
JPanel panel1 = new JPanel();
...
panel1.add(labelPanel1, BorderLayout.WEST);
panel1.add(fieldPanel1, BorderLayout.CENTER);
A JPanel's default layout is flowlayout (left or center, not sure..) Anway, it is no borderlayout so the west and center you give as parameter there have no effect whatsoever.
The 2 components are placed next to eachother yes, but that's cause of the flowlayout. Not the borderlayout
  • 0

#3 onlybarca

onlybarca

    CC Lurker

  • Just Joined
  • Pip
  • 2 posts

Posted 27 November 2010 - 08:29 AM

Great! it works! thank you
  • 0

#4 wim DC

wim DC

    Roar

  • Expert Member
  • PipPipPipPipPipPipPipPip
  • 2681 posts
  • Programming Language:Java, JavaScript, PL/SQL
  • Learning:Python

Posted 27 November 2010 - 08:58 AM

Thank you! It solved my problem, thanks so much, I understand it now.
The centerPanel is in the center of the frame now, how do I put it to the left side?
And also if I want to add new elements like combo box, below the text fields, do I just add them to centerPanel ? and then change:

(new GridLayout(0, 2, 5, 6));
into

(new GridLayout(0, 3, 5, 6));
???


Thank you


I'll answer the combobox question first.
The parameters for the gridLayout stay the same. The first 2 parameters are row and column
so this gridLayout has 0 rows, and 2 columns. By saying 0 rows, java will automatically add rows as more items are added. Items are added from left to right like:
+-----+-----+
|  1  +  2  +
+-----+-----+
|  3  +  4  +
+-----+-----+
|  5  +  6  +
+-----+-----+
|  7  +  8  +
+-----+-----+
So if you add a combobox to the jpanel, it will be at position 5. If you first add a label (goes to 5), and then a combobox it will be at position 6 and the grid will have 3 rows now.


----------------------------
Other question:
If you've added the centerPanel from my code to the panel1 from your code you can:
  • change panel1's layout to borderlayout and do panel1.add(centerPanel, BorderLayout.WEST);
  • change panel1's layout to Flowlayout and do panel1.add(centerPanel); I believe flowlayout by default has a left align.

  • 0





Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download