• 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
  • Paul Clapham
  • Ron McLeod
Sheriffs:
  • Jeanne Boyarsky
  • Liutauras Vilda
Saloon Keepers:
  • Tim Holloway
  • Carey Brown
  • Roland Mueller
  • Piet Souris
Bartenders:

Spreading out JLabels in a panel

 
Ranch Hand
Posts: 41
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Rancher
Posts: 3324
32
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Ranch Hand
Posts: 686
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Ranch Hand
Posts: 52
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Gulshan Singh

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



Layout Manager
 
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
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
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
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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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"...
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic