Skip to main content

JGoodies forms ROCK!

7 replies [Last post]
jacek
Offline
Joined: 2003-08-22
Points: 0

I'm sorry, I just had to start a Forms appreciation thread :-)

Compared to all the GridBagLayout code I had (generated from NetBeans GUI Builder) coding the layout by hand in Forms is so dead easy once you get the hang of it and involves so little code:

JPanel buttonBar = ButtonBarFactory.buildOKCancelBar(jButtonOK,jButtonCancel);

//create the layout
FormLayout layout = new FormLayout(
"pref : grow",
"fill : pref : grow,5dlu,center : pref");
PanelBuilder builder = new PanelBuilder(layout);
CellConstraints cc = new CellConstraints();

builder.setDefaultDialogBorder();
builder.add(directoryChooserPanel,cc.xy(1,1));
builder.add(buttonBar,cc.xy(1,3));

I had about 5-6 times as much grid bag layout code. And I love the ButtonBarFactory class that automatically generates prefefined JPanels for common button sets suck as OK/Cancel, OK/Cancel/Help, etc...plus, if I remember the docs correctly it even switches the button order around to respect each OS's default settings (I think on Mac they are reversed compared to Windows).

Really, yet another great piece of code by Karsten Letzsch. This project should be on the shortlist of all Swing programmers.

Reply viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.
zander
Offline
Joined: 2003-06-13
Points: 0

Since I failed to understand what it should do from the source; I decided to try it.
It gives me an [code]java.lang.IllegalArgumentException: The form spec must not be empty.[/code]
on the constructor of FormLayout. (Forms 1.0.2-RC)

Any idea?

jacek
Offline
Joined: 2003-08-22
Points: 0

You probably have to remove the spaces in "fill : pref : grow", etc. I had to put them in there because the forum software was constantly turning it into smileys.

Check out the JGoodies examples too...it's just much less code and you can make it handle both 96dpi and 120dpi displays automatically when using logical units (e.g. "5dlu") instead of hardcoded pixels.

There's a class I'm working on that has a working example:
https://swingpowerpack.dev.java.net/source/browse/swingpowerpack/src/org...

(look at initComponents)

but if you look at the JGoodies Form demo app you will see much better and more complex examples

zander
Offline
Joined: 2003-06-13
Points: 0

> You probably have to remove the spaces in "fill : pref : grow", etc. I had to put them in there because the forum software was constantly turning it into smileys

use code blocks the next time :)

[code]
ormLayout layout = new FormLayout(
"pref:grow",
"fill:pref:grow,5dlu,center:pref");
[/code]

Karsten made a framework that allows you less code to use the quite-bare widgets Swing provides.
I believe more in finishing the widgets and providing better and more complete widgets.
For example the StandardDialog widget that provides the above with this and much more features, so to reproduce your example I would have to type:
[code]
StandardDialog diag = new StandardDialog(parentWin, "title", StandardDialog.OK_CANCEL);
diag.setComponent(directoryChooserPanel);
[/code]
Sorry, you get a dialog to go with that, but it is still a little shorter :D

jacek
Offline
Joined: 2003-08-22
Points: 0

Looks good too :-)

Isn't diversity fun?

I'm looking at it from the point of a Swing newbie, where Forms allows me to generate layout code fairly painlessly, compared to the rather verbose GridBagLayout syntax.

zander
Offline
Joined: 2003-06-13
Points: 0

> Looks good too :-)
>
> Isn't diversity fun?

Sure is!

> I'm looking at it from the point of a Swing newbie,
> where Forms allows me to generate layout code fairly
> painlessly, compared to the rather verbose
> GridBagLayout syntax.

If you are concerned about short and easy to learn I'm not sure that replacing the learning of GridBag with all the Forms classes is the final idea..

I always use the http://uic.sf.net NaturalLayout that makes this even easier.

[code]
JPanel myPanel = new JPanel(new NaturalLayout(3,2));
myPanel.add(new JButton("OK"), "1,1");
myPanel.add(new JButton("Cancel"), "2,1");
[/code]

And thats about it...

jacek
Offline
Joined: 2003-08-22
Points: 0

Looks interesting. How do you handle specifying which NaturalLayout cells occupies how much space, which one grows/fills, etc?

zander
Offline
Joined: 2003-06-13
Points: 0

> Looks interesting. How do you handle specifying which
> NaturalLayout cells occupies how much space, which
> one grows/fills, etc?

The default 'add' (as you have seen) is a fill, much like the one from FormsLayout. Except that you can have more then one fill row/col in your layout at which time the space will be spread according to the sizing of thw widgets min/pref/max sizes.

If you don't want to use that deafult you can use this:
[code]
layout.setColumn(1, NaturalLayout.MINIMUM);
[/code]
next to minimum you can call that method with PREFERRED or a pixel size.

Widget alignment is passed at add.
panel.add(new JButton("bla"), "1,1,L,C");
which adds it left aligned.