tags:

views:

651

answers:

10

I just had to add a checkbox to an application that was written before I got here, and it was way more difficult than it had to be because the app uses some third-party LayoutManager that attempts to do pseudo-absolute, gridlike positioning. The API was terrible, it takes position-designating strings that are comma-delimited lists of two, four, or six parameters (I still don't know why this varies), and I would much rather let the LayoutManager handle a lot of this grunt work, anyway. I've always felt like allowing Swing to position things itself led to better organization than anything I could generate. I felt the same way with CGI applications, where other than occasionally grouping checkboxes or radio boxes with tables I pretty much just let the browser flow and wrap things however the user wants.

Are the LayoutManager implementations included with Swing adequate, or is it really necessary to incorporate this kind of absolute control to force the layout to be exactly what you want (and give you a million more decisions to make)?

+3  A: 

The layout managers from Swing are generally sufficient to do whatever it is you are trying to do. At the "worst" you will have to use complete GridBagLayout and position each component on your own using coordinates. From your question, it sounds like your company has written a cover for by allowing you to add components using some bizarre input scheme. I would generally recommend not creating a new layout manager unless you can really explain how none of the built-in managers do what you need it to do, nor can they be joined easily to do what you need.

Elie
+6  A: 

This situation is getting a lot better. I would say the three most common non-standard layout managers are:

  • JGoodies FormLayout
  • TableLayout
  • MigLayout

The last two are pretty similar, but MigLayout came slightly later, and will be part of the JDK in future. I'd say any of the three have "standard" enough to use fairly comfortably, but MigLayout is likely to win out now it's to be part of Swing itself.

So at the moment, I'd say no. Once MigLayout's in the JDK, yes.

Draemon
MigLayout is the knockout punch on manual layout managers - highly recommended.
Kevin Day
Are you sure MiGLayout will be part of the JDK in the future? If so, explain why here: http://stackoverflow.com/questions/2092371/is-miglayout-going-to-be-included-in-java-7
Epaga
+1  A: 

I've never been a big fan of Swing layout managers, but I came to Swing from having done a lot of Motif. I'm not sure what it is, but it doesn't seem to me like Swing has the ability for something nested down low to "push out" and force the things higher than it to be big enough to fit them in. Also, I was a really big fan of XmForm and there's nothing like it in Swing.

That said, 90% of the time in Swing I use a bunch of nested Boxes - usually vertical boxes within horizontal boxes within vertical boxes. Sometimes I need more control and I did out the manual for GridBagLayout and shudder.

Paul Tomblin
+3  A: 

In my experience they're adequate providing your prepared to do a bit of nesting (in some cases a lot of nesting!)

Either way I've always found layout managers to be be more trouble than they're worth.

cagcowboy
You wouldn't be saying that if you had to support internationalization. Every time you get a new translation, all your labels have to be resized to fit the new longer strings.
Paul Tomblin
Are you saying you manually place your components at absolute positions?
Draemon
+4  A: 

Personally, I think that the Java layout management concept is the coolest thing since sliced bread; essential for resizeable windows - just look at all the windows in Windows which aren't resizeable as an indication that having an abstract layout manager is a good idea.

That said, the layout managers in Java are adequate, but require a fair amount of nesting to get to where you need to be. That nesting makes changing things cumbersome. Look into BoxLayout and BorderLayout as your staples; use GridBagLayout only as a last resort.

You best option is to incorporate a decent table oriented layout to minimize the nesting; others have already suggested several so I won't reiterate. I rolled my own because I wanted String based constraints and a tiny footprint, but otherwise I was quite tempted by TableLayout; and if distribution size is no object you absolutely must take a look at the widgets and layouts offered by jGoodies (free I think, but perhaps only for non-commercial use??)

I typically use a table layout for the window and end up nested only sometimes and usually just one level deep for runs of related components, say three boxes for a phone number separated by two labels for the dashes in between. I find my windows to be highly maintainable and it's easy to add to them and change them around.

PS: Avoid any absolute positioning like the plague

Software Monkey
Out of curiosity, which of the following meanings of plaque are essential to avoid? http://en.wikipedia.org/wiki/PlaqueCan't quite see the harm in "a biofilm that builds up on teeth" nor "a flat ornamental plate or tablet fixed to a wall, used to mark a significant event, person, etc"=P
Keeg
Took me a second to get it... that's too funny! I corrected the typo.
Software Monkey
MiGLayout has string based constraints...
PhiLho
@PhiLho: MigLayout didn't exist when I wrote mine, or, I didn't find it when I researched alternatives.
Software Monkey
+1  A: 

From your description you may be using TableLayout. If so I have found this to be superior to the standard layout managers in many ways. If it is TableLayout try reading the documentation and you will then understand why there are sometimes different numbers of positioning parameters. TableLayout also allows you to set these parameters in a different way, without the Strings.

A: 

In addition to the 3 abovementioned 3rd party layout managers, you might want to give a try to DesignGridLayout which, although not as powerful as MigLayout, is much easier to use and makes it difficult to design bad forms.

Disclaimer: I am one of the authors of DesignGridLayout, but I really believe this is a good LayoutManager compared with Swing default ones, and also other 3rd-party layouts that are a bit too complicated to start with.

jfpoilpret
+1  A: 

Any comment on GroupLayout? I've read here recently that it is the jgoodies underneath.

OscarRyz
After a frustrating struggle to implement my "just so" layout using SpringLayout, I managed to get it exactly the way I wanted using GroupLayout. I think I wasn't aware of GroupLayout before reading this answer, thank you very much! GroupLayout allows to do nested grid-based layouts without needing nested containers; and it allows specifying sizing constraints on components using their own preferred size. Recommended!
Carl Smotricz
A: 

I've always thought that GridBagLayout is a really powerful LayoutManager. Unfortunately the learning curve to master it might be a bit too steep. My first job did not allow us to use any GUI builders so all GUI code was handwritten. I've always felt that this is the best way to learn GridBagLayout, getting your hands dirty. Nowadays, being old and lazy I mostly use NetBeans but I still have never felt the need to consider other LayoutManagers since GridBagLayout can do everything.

To me also a grid is a really good way to layout things. It just makes sense to organize your layout that way and think of it in terms of rows and columns.

willcodejavaforfood
A: 

I haven't done any Swing work for a couple of years now, but the layout managers are one of the things I miss the most when trying to write web pages. We did alot of work with GridBagLayout especially for the more complicated dialogs and once mastered was relatively easy to use - when writing the screens. The pain always came when having to modify the GUI and what on the surface would seem simple would take a stupid amount of time to get done.

I was introduced to Abeille, a GUI form designing tool, and while not perfect enables you to build screens in a matter of minutes with professional looking results, especially when combining it with JGoodies. It includes a code generator or would output to binary format (I think it included XML later on) which was my preferred option. The code to "build" the screen then looked like this:

FormPanel panel = new FormPanel( "myDialog.jfrm" );
add(panel);

You can hook into the controls on the form for action listeners and when employing an MVC pattern leads to some really clean view code. The biggest benefit comes when making changes tho, as the form can just be loaded into the editor, changes quickly made and saved with no alteration to the code.

(I am in no way associated with Abeille, just a big fan)

MrWiggles