Suggestions for declarative GUI programming in Java

I wonder if there are any suggestions for declarative GUI programming in Java. (I hate visually based graphics / software editors, but I'm a little tired of manually creating JPanels and Boxes, JLabels and JLists, etc.)

Which is my general question, but I have two specific questions for the approaches that I think of:

  • JavaFX: is there somewhere a realistic graphical interface (for example, not circles and rectangles, but a list and buttons and shortcuts, etc.) in JavaFX that can interact with the original Java file that accesses and updates various elements?

  • Plain Old Swing with something to parse XUL-ish XML: Has anyone invented declarative syntax (like XUL) for XML for use with Java Swing? I believe that this would not be easy to do to create some STaX-based code that reads an XML file, creates a hierarchy of Swing elements, and makes the hierarchy accessible through some kind of object model. But I would rather use what is well known and documented and verified than try to come up with one myself.

  • JGoodies Forms is not entirely declarative, but curious, and I got lucky with JGoodies Binding. But their syntax for the layout of the form seems mysterious.

edit: many great answers here! (I added # 3 above). I would be especially grateful that you heard about the experience of any of you who used one of these frameworks for real applications.

ps I tried several search queries ("java gui declarative"), I just didn't know what to look for.

+53
java user-interface layout swing declarative
Apr 17 '09 at 16:52
source share
14 answers

You can see javabuilders ; it uses YAML to create Swing user interfaces.

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 

As an alternative:

 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) 
+22
Apr 17 '09 at 16:56
source share

As the author of CookSwing, a tool that does what you need, I gave this topic a long look before doing the actual implementation. I made my living as a Java Swing GUI application.

IMO, if you intend to use any imperative programming languages ​​to describe the Java Swing component, you can simply use Java. Groovy etc. only adds complexity without much simplification.

Declarative languages ​​are much better, because even non-programmers can understand this, especially when you need to delegate the task of fine-tuning specific layouts to artists. XML is ideal for declarative languages ​​(compared to other options) because of its simplicity, readability, and the many editors and translation tools available for editing.

These are the problems that are encountered in a declarative graphical interface, and not in any particular order. These issues were addressed at CookSwing.

  • Readability and simplicity. (JavaFX is no simpler than XML). Closing XML tags helps to read a little, and does not add extra typing, as XML editors usually do this for you)
  • extensibility. Very important, because custom Swing components are suitable for any non-trivial projects.
  • GUI layouts. Also very important. Ability to manage BorderLayout, GridBagLayout, JGoodies FormsLayout, etc. Almost required.
  • Easy to copy / paste. In the design process, you need to try different ones. Therefore, you need to be able to copy / paste and move things. XML is better because the hierarchy of components and layouts is 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 many good XML editors.
  • Templates (i.e. the ability to add another layout file) are very useful for sequential viewing. For example, you may need a constant view of dialog boxes, button panels, etc.
  • Interaction with Java code. It is important. Some GUI components can only be created using Java code (for some reason). Therefore, you must be able to load these objects. It is also a must to directly connect listeners and other Java objects / components in XML code. Using ids to connect them later, WILL will not work, as it is very tiring.
  • Internationalization (i18n). Ability to load text / line from a set of resources, rather than hard-coded text. This feature may be critical for some applications.
  • Localization (l10n). The advantage of declarative programming (in particular XML) is that you can simply switch to another form of GUI for a specific locale and its. If you code Java or any other imperative languages, this is not so simple.
  • Error checking / tolerances. Initial designs often contain errors here and there. Sometimes an error can be caused by the fact that the corresponding Java code has not yet been developed. Or missing icon resource. Dealing with imperative coding errors is extremely tedious. Thus, it is desirable to be able to find errors, but at the same time be error tolerant, so you can preview the GUI layout as early as possible.
  • GUI component replacement. That is, replace the text box that used the JTextField with some more convenient version of the components. Replace the dialog value with some fancy user interface dialogs (like JIDE) instead of JDialog. This feature can save significant effort. XML itself is also useful thanks to XSLT and other conversion tools.
  • Outside of the swing. Since sooner or later you will find many component configurations, use object types such as arrays, icons, images, vectors, etc.
+19
May 28 '09 at 12:41
source share

If brevity is important, you can consider double-bracket idioms:

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

Then you do not lose the ability of the well-known general-purpose programming language (you know that you will need it, and JellyTags suck). All you need is another additional idiom.

It was not used very much, because in fact, people who wrote with XML did not solve real pain points.

In general, you can use builder layers for abstract re-code. The GUI code should not be poorly written, it is just that almost all of this (including in text books).

+11
Apr 17 '09 at 19:38
source share

I highly recommend the MiG layout - it takes several days for the syntax, but once you get it, it works wonders. I used JGoodies Forms for quite a while, and the Karsten builder concept works well, but it's a bit cryptic ... MiGs are easier to pick up and produce remarkably compressed code.

+10
Apr 18 '09 at 5:15
source share

If you want to go a little beyond simple Java, Groovy , then the concept of "builder" works very well with graphical interfaces. Of course, you can interact quite easily between Groovy and Java. See Swing Builder for more details.

+7
Apr 17 '09 at 16:55
source share

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

"Swiby is a mix of Swing and Ruby for truly rich distributed applications." In other words, Swiby is a domain-specific language that mixes swing and ruby.

+2
Apr 17 '09 at 16:55
source share

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

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

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

+2
Jun 08 '10 at 13:25
source share

I can find the following examples of what you are asking:

+1
Apr 17 '09 at 16:55
source share

Although not declarative and limited solely to layouts, you can take a look at DesignGridLayout , which allows you to programmatically define Swing layouts in a very concise way (this is open source).

Main advantages:

  • easy and quick to learn.
  • compressed code (1 line of code for the line of components in the form), which also facilitate maintenance
  • checking compilation time (which a declarative user interface cannot be)
  • respect for the perception of the platform (alignment of the baseline, the gaps between the components ...) without any hard-coded length value
+1
Apr 20 '09 at 3:18
source share

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

+1
May 26 '09 at 20:57
source share

I recently met SDL / Swing .

+1
Apr 26 '10 at 23:27
source share

I have tried many solutions like SWIXML, Javabuilders, MigLayout, Cookswing. I finally found javaFX and javaFX-Scenebuilder the best of the fastest XML based GUI solutions. you would like the script to create a graphical interface (with drag and drop elements!). plus, it uses CSS (cascading style sheets) for a GUI theme. Jsut trusts Oracle, it is the best GUI tool for Java applications. take a tour of creating javaFX applications using a screenwriter, here: http://docs.oracle.com/javafx/scenebuilder/1/get_started/prepare-for-tutorial.htm#CEGJBHHA

+1
Jul 03 '13 at 17:00
source share

As often, it is always useful to do a search when you are looking for something. This is the first link in google looking for "java xml gui"

0
Apr 17 '09 at 16:57
source share

WindowBuilder is a very good plugin including GWT, XWT, SWT, Swing, etc.

0
Jan 11 2018-12-12T00:
source share



All Articles