• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • paul wheaton
  • Jeanne Boyarsky
  • Ron McLeod
Sheriffs:
  • Paul Clapham
  • Liutauras Vilda
  • Devaka Cooray
Saloon Keepers:
  • Tim Holloway
  • Roland Mueller
Bartenders:

aligning label and fields in Swing

 
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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:

 
Bartender
Posts: 11497
19
Android Google Web Toolkit Mac Eclipse IDE Ubuntu Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.

 
Ranch Hand
Posts: 54
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
use grid layout with gridbag constraints..that way you have alot more control over your placements in the panel.
 
Sheriff
Posts: 22822
132
Eclipse IDE Spring Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Space pants. Tiny ad:
Smokeless wood heat with a rocket mass heater
https://woodheat.net
reply
    Bookmark Topic Watch Topic
  • New Topic