aspose file tools*
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 EJB 3 in Action this week in the EJB and other Java EE Technologies 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: 39535
    
  27
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 - updated with new look and Ping home screen widget
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: 15286
    
    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 - A Test Data Generation Platform
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
 
Similar Threads
DRAW A NEW LINE
Just finished my first GUI program (a planner application) ... could you give me feedback?
How to send a request without leaving the current page?
Is MiGLayout the best layout manager?
gmail like custom "select all" control