aspose file tools*
The moose likes Swing / AWT / SWT and the fly likes Spreading out JLabels in a panel Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "Spreading out JLabels in a panel" Watch "Spreading out JLabels in a panel" New topic
Author

Spreading out JLabels in a panel

Gulshan Singh
Ranch Hand

Joined: Apr 22, 2009
Posts: 41
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

Joined: Jun 13, 2009
Posts: 2200
    
    7
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

Joined: May 13, 2009
Posts: 679
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

Joined: Jun 16, 2009
Posts: 52
Gulshan Singh

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



Layout Manager

At the center of non-violence stands the principle of love
.... Martin Luther King, Jr.
Maneesh Godbole
Saloon Keeper

Joined: Jul 26, 2007
Posts: 10451
    
    8

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.


[How to ask questions] [Donate a pint, save a life!] [Onff-turn it on!]
Maneesh Godbole
Saloon Keeper

Joined: Jul 26, 2007
Posts: 10451
    
    8

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

Joined: Jun 16, 2009
Posts: 52
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"...
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Spreading out JLabels in a panel