• 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Paul Clapham
  • Ron McLeod
  • Liutauras Vilda
  • Bear Bibeault
Sheriffs:
  • Jeanne Boyarsky
  • Tim Cooke
  • Devaka Cooray
Saloon Keepers:
  • Tim Moores
  • Tim Holloway
  • Piet Souris
  • salvin francis
  • Stephan van Hulst
Bartenders:
  • Frits Walraven
  • Carey Brown
  • Jj Roberts

Simple swing form layout

 
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
Am trying to design a simple GUI which is basically a form with labels down one side and various components down the other all nicely aligned.

This seems to be proving really difficult and I'm sure this kind of layout is a common one.
Is there an easy way of arranging components into a simple form?
Maybe someone could point me in the right direction.

Thanks.
 
Rancher
Posts: 43016
76
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I found the LabelledItemPanel class discussed in this article to be very useful for exactly that kind of layout.
[ May 25, 2008: Message edited by: Ulf Dittmer ]
 
Ranch Hand
Posts: 82
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Originally posted by Matthew Plant:
Hi,
Am trying to design a simple GUI which is basically a form with labels down one side and various components down the other all nicely aligned.

This seems to be proving really difficult and I'm sure this kind of layout is a common one.
Is there an easy way of arranging components into a simple form?
Maybe someone could point me in the right direction.

Thanks.



I've used the JGoodies Forms layout manager a lot in the past and it works really well, especially for form type layouts. Try it at http://www.jgoodies.com/freeware/forms/index.html.

Dave
 
Ranch Hand
Posts: 15304
6
Mac OS X IntelliJ IDE Chrome
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I really like MigLayout because it is coder friendly rather than tool friendly. To get the desired layout it is really this simple:



A new MigLayout with a "fill" property is created. The "fill" property tells it that when the ui is resized, the elements should fill the panel. The new JTextFields have growx which tells "fill" to grow the textfields but only on the x plane, rather than y (vertically). "wrap" tells the layout that the next element should appear on the next line.

There are a lot of different ways to use MigLayout. It is very powerful.
 
Bartender
Posts: 1561
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You can do what you desire (if I understand you correctly) by nesting layouts:

* Outer layout is BorderLayout.
* BorderLayout.WEST JPanel uses GridLayout(0, 1). The Labels go here
* BorderLayout.CENTER JPanel uses GridLayout(0, 1) as well, and the components go here.

Often there is further layering that must be done, but with a little tweaking, it can work quite well. While it may not be as straightforward as the posts listed above, it is useful in situations where you want to share your code with others (such as this forum) but not want to use non-standard components.
 
When it is used for evil, then watch out! When it is used for good, then things are much nicer. Like this tiny ad:
Thread Boost feature
https://coderanch.com/t/674455/Thread-Boost-feature
reply
    Bookmark Topic Watch Topic
  • New Topic