Saturday, April 26, 2008

Java Builder for Swing: a complete complex layout sample

Had a chance to code a bit more on my vacation during the last 2 weeks, so the Java Builders project has made a big step forward.

I took the ZoneLayout sample from a previous post and re-created it (but using label separators instead of nested JPanels):

with full proper support for resizing:


with included support for internationalization and with the buttons hooked up via action commands to the main frame (e.g. clicking the OK button):


First, the resource file with all the internationalized content:
http://code.google.com/p/javabuilders/source/browse/trunk/org.javabuilders.swing/test/Resources.properties

Next the actual YAML file with the layout and properties:
http://code.google.com/p/javabuilders/source/browse/trunk/org.javabuilders.swing/samples/org/javabuilders/swing/samples/TestFrame.yaml

and here's the Java JFrame that builds itself from this file:
http://code.google.com/p/javabuilders/source/browse/trunk/org.javabuilders.swing/samples/org/javabuilders/swing/samples/TestFrame.java

Let's have a quick look at the YAML file and go over some of the details of the visual layout part (which uses MigLayout under the hood) to explain some of the parameters:
sepName+*     : the "+" is for horizotal span, "*" indicates full remainder of the row
>fNameLabel : the ">" is to indicate the label should be right aligned
fNameField=1 : the "=1" defines a size group, all controls that equal to the same number
should be of the same size
textArea+4+3 : "+4+3" indicates span 4 columns, 3 rows

So, any comments? Suggestions?

11 comments:

Andres Almiray said...

Looking good! I must say if I have to go back to making UIs in plain Java I would definitely use JavaBuilder. It has a short syntax and harness the power of MigLayout, great!

Gregg Bolinger said...

Looks interesting. Is it possible to start at the JPanel level instead of the JFrame? I'd rather just build forms and not have it tied directly to a JFrame in the YAML file. And I'd also need to be able to have multiple YAML files, one for each form, and be able to load those on the fly.

Victor said...

This is great! But how can I have access to the swing components declared in the YAML file?

I dont know if this is possible, but it would be awesome if we could declare this in TestFrame

public JLabel fNameLabel;

And then Builder.build(...) would populate this attribute with the JLabel declared in the YAML file with the same name

Jacek Furmankiewicz said...

Andres: nothing stops you from using it in Groovy, right? :-)

Gregg: absolutely. You can put ANY recognized component as the root, zero restrictions on this. JFrame, JPanel, JMenuBar, etc.

victor: absolutely. The return of the build() command is a BuildResult object:
http://code.google.com/p/javabuilders/source/browse/trunk/org.javabuilders.core/src/org/javabuilders/BuildResult.java?r=60

getByName() is what you are looking for...

Jacek Furmankiewicz said...

victor: just a follow up, right now you have to get a reference manually. I plan to look at auto-populating the variable...but they are usually private, so I am not sure if I can set them via reflection (this project is giving a crash course in reflection, although mostly I use Apache Commons BeanUtils under the hood).

Also, I plan to look at integrating Beans Binding to allow binding of control properties in a JSF style, e.g

JButton: {name: firstName, text: $(firstName)}

that would alleviate the need to get a reference, since you would automatically just bind the important attributes to a property on the root object...

Gregg Bolinger said...

"but they are usually private, so I am not sure if I can set them via reflection"

You can get private members via reflection.

"Also, I plan to look at integrating Beans Binding to allow binding of control properties in a JSF style, e.g

JButton: {name: firstName, text: $(firstName)}"

This isn't specific to JSF. This is just EL binding, if you want to call it that. I can't remember the link but GlassFish seperated their EL API into its own project to be used with anything so you might hunt that down. It might save you some work.

Aside from that, still looking good.

Andres Almiray said...

Regarding binding and EL, if you only need an EL impl you may also use commons-jexl instead of glassfish's El. If you really need binding not just El, then BeansBinding (JSR295) is a good choice.

Regarding private access and refleciton, don't forget to call setAccessible() :-)

And yes, I can use JavaBuilder with Groovy if needed be, it is just that wiring behavior via closures is so much easier in "plain" Groovy ;-)

Keep it up!

Jacek Furmankiewicz said...

Ah, closures, yes :-)

Thanks for your words of encouragement...I will work on Victor's suggestion as my top priority, I think it will really kickstart the builder's usefuleness...

Jacek Furmankiewicz said...

Victor: it's done. The builder now autopopulates the properties on the calling object, as long as it's not null, has same name and is of compatible type.

Thanks for Andres for the getAccessible(), hint...I would have lost a lot of time without it.

See here for the working code:
http://code.google.com/p/javabuilders/source/browse/trunk/org.javabuilders.core/test/org/javabuilders/test/TestFrame.java

and here for the implementation that gets called automatically:
http://code.google.com/p/javabuilders/source/browse/trunk/org.javabuilders.core/src/org/javabuilders/BuildResult.java

(last method: updateNamedObjectReferencesInCaller())

Jacek Furmankiewicz said...

And now I've created a quick and dirty drop of the code for you gentlemen to play around with.

Go to javabuilders.org, featured download on the right. Let me know what you come up with with this *very early* pre-release.

Don't use this in production, for heaven's sake, OK? :-)

Anonymous said...

You need to go through long distances sometimes, making you buy a lot of Teleport scrolls with your Rose zuly just to go to the cities. The quests are OK and have a good storyline so far, and you will gain a good amount of experience by grinding and rose zulie too. Something I liked in this game was the nice design of the clothes can have from rose online zuly. There also armor also can gain from rose online zulie sets that can be wore by any class. Another nice thing that I would like to talk about, is that you can have nice Arua ROSE zuly in ROSE Online.