wood burning stoves 2.0*
The moose likes Swing / AWT / SWT and the fly likes Simple swing form layout Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of OCM Java EE 6 Enterprise Architect Exam Guide this week in the OCMJEA forum!
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "Simple swing form layout" Watch "Simple swing form layout" New topic
Author

Simple swing form layout

Matthew Plant
Greenhorn

Joined: May 07, 2008
Posts: 18
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
Marshal

Joined: Mar 22, 2005
Posts: 41621
    
  55
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 ]

Ping & DNS - my free Android networking tools app
David Irwin
Ranch Hand

Joined: Mar 25, 2004
Posts: 82
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

Joined: Jul 11, 2001
Posts: 15299
    
    6

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.


GenRocket - Experts at Building Test Data
pete stein
Bartender

Joined: Feb 23, 2007
Posts: 1561
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.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Simple swing form layout