suggestions for declarative GUI programming in Java

JavaSwingUser InterfaceLayoutDeclarative

Java Problem Overview


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.

Java Solutions


Solution 1 - Java

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)

Solution 2 - Java

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.

Solution 3 - Java

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).

Solution 4 - Java

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.

Solution 5 - Java

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.

Solution 6 - Java

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.

Solution 7 - Java

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.

Solution 8 - Java

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

Solution 9 - Java

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 and quick to learn.
  • 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

Solution 10 - Java

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

Solution 11 - Java

I recently come across SDL / Swing.

Solution 12 - Java

I've tried many solutions, such as SWIXML, Javabuilders, MigLayout, Cookswing. I finally found the javaFX and javaFX-Scenebuilder the best an fastest solution, XML-based GUI tool. you'd like the way scenebuilder creates GUI (with drag & drop items!). plus, it uses CSS (Cascading Style Sheets) for the GUI theme. Jsut trust the Oracle, it's the best GUI tool for java applications. take a tour for creating javaFX apps with scenebuilder, here: http://docs.oracle.com/javafx/scenebuilder/1/get_started/prepare-for-tutorial.htm#CEGJBHHA

Solution 13 - Java

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"

Solution 14 - Java

WindowBuilder, it a very nice plugin, which included GWT,XWT,SWT,Swing etc

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionJason SView Question on Stackoverflow
Solution 1 - JavaMichael MyersView Answer on Stackoverflow
Solution 2 - JavacoconutView Answer on Stackoverflow
Solution 3 - JavaTom Hawtin - tacklineView Answer on Stackoverflow
Solution 4 - JavaKevin DayView Answer on Stackoverflow
Solution 5 - JavaJon SkeetView Answer on Stackoverflow
Solution 6 - JavapaweloqueView Answer on Stackoverflow
Solution 7 - JavaDaniel LeuckView Answer on Stackoverflow
Solution 8 - JavaEddieView Answer on Stackoverflow
Solution 9 - JavajfpoilpretView Answer on Stackoverflow
Solution 10 - Javax xView Answer on Stackoverflow
Solution 11 - JavaJonasView Answer on Stackoverflow
Solution 12 - JavaShayan_AryanView Answer on Stackoverflow
Solution 13 - JavagizmoView Answer on Stackoverflow
Solution 14 - JavaHeyJoyView Answer on Stackoverflow