Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Cloud/Virtualization forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Spreading out JLabels in a panel

 
Gulshan Singh
Ranch Hand
Posts: 41
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Here's my code:

If you run it you'll see how the labels don't match up with the text areas... I'm very bad at position components using swing, so I was wondering if anyone could help me find a way to line them up. I have all my JLabels in one panel and my JTextFields in another because that was the easiest way to get them side by side like that.
 
Rob Camick
Ranch Hand
Posts: 2540
9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Read the Swing tutorial on Usint Layout Managers. You could use two panels each with a GridLayout. Or you could use a single panel with a SpringLayout. In both case you will need to use a dummy component for your text field that doesn't have a related label. Box.createVerticalStrut() can be used for this.
 
Fred Hamilton
Ranch Hand
Posts: 684
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This article helped me a lot with layout management, when the tutorial was a bit of a struggle.

http://java.sun.com/developer/onlineTraining/GUI/AWTLayoutMgr/shortcourse.html
 
Ramses Butarbutar
Ranch Hand
Posts: 52
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Gulshan Singh

To make you have imagination at position components using swing, maybe this will help :



Layout Manager
 
Maneesh Godbole
Saloon Keeper
Posts: 11021
12
Android Eclipse IDE Google Web Toolkit Java Mac Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Layout managers were created for a reason. Using null layout and specifying the absolute location of components will land you into all kinds of trouble and give you immense grief in the end.
 
Maneesh Godbole
Saloon Keeper
Posts: 11021
12
Android Eclipse IDE Google Web Toolkit Java Mac Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Gulshan,

You can either get your UI straight with the Gridbag layout (it takes some time to get used to it) or check out Mig Layout
 
Ramses Butarbutar
Ranch Hand
Posts: 52
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Maneesh Godbole wrote:Layout managers were created for a reason. Using null layout and specifying the absolute location of components will land you into all kinds of trouble and give you immense grief in the end.


Yes Maneesh, I'm totally agree with you, set layout into null is suicide.. I just wanna show how to play with component coordinates... Thanks for "the light"...
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic