views:

1741

answers:

12

I wonder if there are any suggestions for declarative GUI programming in Java. (I abhor visual-based GUI creator/editor software, but am getting a little tired of manually instantiating JPanels and Boxes and JLabels and JLists etc.)

That's my overall question, but I have two specific questions for approaches I'm thinking of taking:

  1. JavaFX: is there an example somewhere of a realistic GUI display (e.g. not circles and rectangles, but listboxes and buttons and labels and the like) in JavaFX, which can interface with a Java sourcefile that accesses and updates various elements?

  2. Plain Old Swing with something to parse XUL-ish XML: has anyone invented a declarative syntax (like XUL) for XML for use with Java Swing? I suppose it wouldn't be hard to do, to create some code based on STaX which reads an XML file, instantiates a hierarchy of Swing elements, and makes the hierarchy accessible through some kind of object model. But I'd rather use something that's well-known and documented and tested than to try to invent such a thing myself.

  3. JGoodies Forms -- not exactly declarative, but kinda close & I've had good luck with JGoodies Binding. But their syntax for Form Layout seems kinda cryptic.

edit: lots of great answers here! (& I added #3 above) I'd be especially grateful for hearing any experiences any of you have had with using one of these frameworks for real-world applications.

p.s. I did try a few google searches ("java gui declarative"), just didn't quite know what to look for.

+5  A: 

If you're willing to step slightly outside plain Java, Groovy's "builder" concept works pretty well with GUIs. Of course you can interop between Groovy and Java fairly easily. See the Swing Builder page for more information.

Jon Skeet
+1  A: 

I can find the following examples of what you're asking for:

Eddie
great! Now I just need to figure out which of these is the most "mainstream"...
Jason S
+2  A: 

give Swiby a try: http://swiby.codehaus.org/

"Swiby is a blend of Swing and Ruby for truly rich distributed applications." In other words Swiby is a domain specific language mixing swing and ruby.

lewap
+12  A: 

You might have a look at javabuilders; it uses YAML to build Swing UIs.

A simple example from the manual [PDF]:

JFrame:
    name: myFrame
    title: My Frame
    content:
        - JLabel:
            name: myLabel2
            text: My First Label
        - JLabel:
            name: myLabel2
            text: My Second Label

Alternatively:

JFrame:
    name: myFrame
    title: My Frame
    content:
        - JLabel: {name: myLabel2, text: My First Label}
        - JLabel: {name: myLabel2, text: My Second Label}

Or even:

JFrame(name=myFrame,title=My Frame):
    - JLabel(name=myLabel2, text=My First Label)
    - JLabel(name=myLabel2, text=My Second Label)
Michael Myers
I'll probably go with javabuilders, it seems to score well under my some of my implicit criteria: decent documentation/tutorial, full-featured (w/ bindings, layout manager hooks, etc).
Jason S
A: 

As often, it's always a good idea to perform a search when you're looking for something. This is the first link in google while looking for "java xml gui"

gizmo
good point. You have to know what to search for, though. I googled "java declarative gui", dunno why I didn't think about using "xml" as a search term.
Jason S
@Jason S: +1, I think indicating google searches that didn't seem to work for you is a good idea for what information to add when asking a question.
Arnold Spence
+6  A: 

If conciseness is important you might want to consider the double brace idiom:

new JFrame("My Frame") {{
    setName("myFrame");
    add(new JLabel("My First Label") {{
         setName("myLabel2");
    }};
    add(new JLabel("My Second Label") {{
         setName("myLabel2");
    }};
}}

You then don't lose any of the power of a well known general purpose programming language (you know you are going to need it, and JellyTags suck). All you need is the one little extra idiom.

It's not used very much, because actually people pissing around with XML weren't solving real pain points.

In general you can use builder layers to abstract repeated code. GUI code doesn't have to be badly written, it's just that almost all of it is (including in text books).

Tom Hawtin - tackline
huh - that's actually pretty slick. Nice.
Kevin Day
There's a fine line between slick and nasty. :)
Tom Hawtin - tackline
Eclipse reformats this into something which requires a LOT of lines :(
Thorbjørn Ravn Andersen
@Thorbjørn Don't let it reformat your code then.
Tom Hawtin - tackline
@Tom, we have Format Source as a default save action in Eclipse so source code is normalized at all times. This is to give best possible conditions to maintainers needing to look back in the repository history.
Thorbjørn Ravn Andersen
+7  A: 

I strongly recommend MiG Layout - it takes a few days to get used to the syntax, but once you've got it, it works wonders. I used JGoodies Forms for quite awhile, and Karsten's builder concept works well, but it is a bit cryptic... MiG is easier to pick up, and results in wonderfully concise code.

Kevin Day
thanks -- mmyers's recommendation for Javabuilders also led me to MiG Layout.
Jason S
I just dropped using GUI builders (VisualBuilder) because of MigLayout :)
jb
looks very neat, and someone's even written a clojure module to wrap it. thanks for the pointer.
Martin DeMello
A colleague cannot stop praising MIG Layout.
Thorbjørn Ravn Andersen
A: 

Although it is not declarative and is limited exclusively to layouts, you might want to take a look at DesignGridLayout which allows to programmatically define Swing layouts in a very concise manner (it's open source).

Main advantages:

  • easy to learn (you can start using it for real forms after just one hour)
  • concise code (1 line of code per row of components in a form) that also enable easy maintenance
  • compile-time checking (which declarative UI can't have)
  • respect of platform look & feel (baseline alignment, gaps between components...) without any hard-coded length value
jfpoilpret
A: 

something new...XWT, will be included in eclipse e4

+8  A: 

As the author of CookSwing, a tool that does what you need, I've given this subject a long hard look before doing the actual implementation. I made a living writing Java Swing GUI applications.

IMO, if you are going to use any kind of imperative programming languages to describe Java Swing component, you might as well just use Java. Groovy etc only adds complications without much simplification.

Declarative languages are much better, because even non-programmers can make sense out of it, especially when you need to delegate the task of fine tuning of specific layouts to artists. XML is perfect for declarative languages (over other choices) because of simplicity, readability, and plenty of editors/transformation tools etc available.

Here are the problems faced in declarative GUI programming, not in any particular order. These issues have been addressed in CookSwing.

  1. Readability and simplicity. (JavaFX is not any simpler than XML. Closing tags of XML helps reading quite a bit, and doesn't add extra typing much since XML editors usually do it for you)
  2. Extensibility. Very important, because custom Swing components will come up for any non-trivial projects.
  3. GUI layouts. Also very important. Being able to handle BorderLayout, GridBagLayout, JGoodies FormsLayout, etc are practically a must.
  4. Simplicity of copy/paste. In the course of the designing the layout, it is necessary to try out different ones. So one need to be able to copy / paste and moving things around. XML is better because the hierarchy of components and layouts are easy to see. JavaFX is somewhat problematic due to multi-line attributes and indentation issues. Having a good editor is a must, and there are plenty of good XML editors.
  5. Templates (i.e. being able to include another layout file) is very useful for consistent look. For example, one might want to have a consistent look of dialogs, button panels, etc.
  6. Interactions with Java code. This is crucial. Some GUI components can only be created with Java code (for whatever the reason). It is thus necessary to be able load these objects. It is also necessarily being able to directly hook up listeners and other Java objects/components within the XML code. Using ids to hook them up later WILL not work well, as it is very tedious.
  7. Internationalization (i18n). Being able to load text / string from a resource bundle rather than hard coded text. This feature can be crucial for some applications.
  8. Localization (l10n). The advantage of declarative programming (particularly with XML) is that you can just switch to a different GUI form for a specific locale and that's it. If you code with Java or any other imperative languages, it is not so easy.
  9. Error check / tolerance. Initial designs often will contain errors here and there. Sometimes the error might be because the corresponding Java code hasn't been designed yet. Or an icon resource is missing. Dealing with errors with imperative coding is extremely tedious. Thus it is desirable to be able to locate the errors, yet at the same time being error tolerant, so the preview of the GUI layout can be made as early as possible.
  10. GUI component replacement. That is, replace textfield which used to have JTextField with some fancier version of components. Replace the meaning of dialog with some fancy UI dialogs (such as JIDE's) instead of JDialog. This feature can save significant amount of efforts. XML itself is also useful due to XSLT and other transformation tools.
  11. Beyond Swing. Because sooner or later you will find many component configurations use object types such as arrays, icons, images, vectors, etc.
interesting....
Jason S
"I have a tool that can do this, and here are the things I had to think about" is much more useful than "I have a tool that can do this, you should use it because it's great." +1
MatrixFrog
+1 for experiences.
Thorbjørn Ravn Andersen
+1  A: 

I recently come across SDL / Swing.

Jonas
A: 

SDL/Swing does exactly what you need. Its a tiny (283k), unobtrusive, easy to learn declarative Swing framework.

menus {
    "File" {
        "Open" do="open" // calls "open()" in the controller
        "---"
        "Exit" do="exit"
    }
}

SDL/Swing is open source but enjoys commercial support. We (Ikayzo.com) developed it over a period of years and have deployed it in production systems for many customers ranging from life science companies to banks.

Daniel Leuck