This week's book giveaways are in the Refactoring and Agile forums.
We're giving away four copies each of Re-engineering Legacy Software and Docker in Action and have the authors on-line!
See this thread and this one for details.
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

Simple swing form layout

 
Matthew Plant
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.
 
Ulf Dittmer
Rancher
Pie
Posts: 42967
73
  • 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 ]
 
David Irwin
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
 
Gregg Bolinger
GenRocket Founder
Ranch Hand
Posts: 15302
6
Chrome IntelliJ IDE Mac OS X
  • 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.
 
pete stein
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic