Win a copy of Murach's MySQL this week in the JDBC and Relational Databases forum!
  • 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 ...
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
  • Mikalai Zaikin

Regarding layouts

Ranch Hand
Posts: 35
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi There

I want to construct a Swing GUI where I want to place labels and controls in a 4X2 matrix. I want to give sufficient spaces in between so that the GUI looks great.

I wanted to have only one Label and its corresponding control like JTextBox etc in a single line, for the next Label and control I should move to next line.

I am sure it is very common problem.

Is there any standard solution for that?

Posts: 22778
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Ifm you want to torture yourself ( ;) ) you could look at the GridBagLayout and its partner in crime GridBagConstraints. They're quite a pain to work with.

If all labels and text fields can be equally large you can use a GridLayout. This layout, which can take a parameter in the constructor for the gap, can display all components in a sort of table. Each of the cells of the table is equally large, and filling the complete size of the parent container. So if your panel resizes, all these cells resize as well. I wouldn't recommend this layout if your panel can resize.

My favourite layout manager is the FormLayout from Like the GridLayout it creates a table inside your panel, but you can specify a lot better for each row and column what the size should be. GridBagLayout can do the same but is much more complex.
An example:

Some comments: each column specification is separated by a comma. pref means that the preferred size of the largest component is used. All other components are resized. fill:pref:g means pref, but if the panel resizes this control resizes along. The 1dlu is the default spacing in FormLayout GUIs; you can omit it (meaning no gaps between the controls) or increase it.

Unlike arrays, cells numbering starts at 1. For the rest it's quite similar to arrays. You can also specify that a component should stretch over multiple cells; use cc.xywh(1, 1, 3, 1) to stretch over the first three columns of the first row.

Java 1.6 introduced the GroupLayout which may also do what you want, but I haven't used it yet.

[ November 02, 2007: Message edited by: Rob Prime ]
[ November 02, 2007: Message edited by: Rob Prime ]
Consider Paul's rocket mass heater.
    Bookmark Topic Watch Topic
  • New Topic