Win a copy of Mesos in Action this week in the Cloud/Virtualizaton forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

aligning label and fields in Swing

 
choubix alex
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi!

I am creating a login screen to get up to speed with Swing.
When I run the following code all the elements are displayed BUT their position on the screen is not correct.
is it possible to display the elements like this

username: (textfield here)
password: (passwordfield here)
comment: (comment field here)

etcetc

while aligning them so it looks nice? (all the labels / fields aligned)

thank you for your time

ps: please feel free to comment my code on my code as this is new for me and that I would like to progress fast


code:

 
Maneesh Godbole
Saloon Keeper
Posts: 11027
12
Android Eclipse IDE Google Web Toolkit Java Mac Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What you need is LayoutManagers.
Recommended reading: http://java.sun.com/docs/books/tutorial/uiswing/layout/using.html
Also google for MigLayout Some people feel it is easier to use than GridBagLayout

Since you are starting to learn, I strongly recommend, do not use drag and drop style tools. Hand coding has numerous advantages. Search the forums for details on this topic. It has been discussed many times before.
 
choubix alex
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks again!

I've stumble on MiG shortly after posting my message and I have managed to position my labels in a much better way.
I was tempted to use a Visual builder (netbeans provides one I think) but I have decided to start coding my UI instead so that I can learn better/more.

 
Mayur Gosalia
Ranch Hand
Posts: 54
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
use grid layout with gridbag constraints..that way you have alot more control over your placements in the panel.
 
Rob Spoor
Sheriff
Pie
Posts: 20531
54
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Mayur Gosalia wrote:use grid layout with gridbag constraints..

Don't you mean GridBagLayout, instead of GridLayout? Because GridLayout doesn't use any constraints, it just uses the largest width and height for all components.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic