Skip to Main Content

Java SE (Java Platform, Standard Edition)

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

Align elements inside a JPanel

fcojavierperezSep 17 2009 — edited Sep 17 2009
Hi everybody,

Here it is my question. I have a JPanel inside which there are two things: a JLabel and a JTextField.

I want that the JLabel is placed just as close as posible to the left side of the JPanel, and the JTextField just after it.

So, something like:

|JLabel(default space in a FlowLayout() between components)JTextField |

representing the vertical lines the horizontal limits of the JPanel.

Here it is the code that I have tried but it did not work:
panel = new JPanel();
panel.setLayout(new FlowLayout());
JLabel label = new JLabel("xxx");
label.setAlignmentY(0.0f);
panel.add(label);
panelRM.add(jTextFieldName);  
//being jTextFieldName the JTextField I talked about above. It is created in the constructor.
I have tried also "label.setAlignmentX(0.0f)" because actually I did not know whether I had to use setAlignmentX or setAlignmentY... but it did not work either.

Thanks to everybody!
Fran.
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on Oct 15 2009
Added on Sep 17 2009
4 comments
554 views