JavaFX composer, dynamic layout and XScene

As I mentioned in a previous post, I was quite unsuccessful developing a resizable application with dynamic layout behavior using the beta release of the Netbeans composer. An Amy Fowler's post about Growing, Shrinking and Filling reminded me that I should give another try - dynamic sizing behavior is a great thing.
Moreover, some interesting changes have been introduced in the RC1 version of Netbeans. Let's see how to do this:
  • Create a new design file. By default the design file creates a javafx.scene.Scene. Unfortunately, this scene does not dynamically resize it's child nodes to it's current size.  This is exactly what the org.jfxtras.scene.XScene does. However, in the beta version of Netbeans Composer it was not possible to replace the default Scene by the one provided by JFXtras. Netbeans RC1 has introduced a cool feature: the Class Name Property. This allows to customize the class used in the generated code for a given component. This is a very interesting feature, bringing flexibility without overloading the composer's interface. This way you can customize any component handled by the composer (as long as it's using the same properties). In our case, just set "org.jfxtras.scene.XScene" as class scene for the scene component.
  • Next step is to add a layout manager to the scene. Let's use the Grid container, which is now supported by the RC1 version of the Composer! You can then add components to the layout and manage the number of rows and columns of the Grid layout. Notice that a new property has been introduced in the layout properties of the components: "Grid Hor. Span". This allows to control the spanning of a component over multiple columns. For example, in the attached screenshots, the bottom button is spanned over two columns. The left button switches it's Vertical Fill property, automatically adapting it's size to the new constraints.

You can now start building your application with dynamic sizing and layout using the JavaFX Composer - a big step forward!

(download)

Developing a business application with JavaFX Composer

When we started developing our first business application, I've hesitated a long time whether we should give a try to the Netbeans JavaFX composer. I am not a big fan of such gui design tools, especially because you usually get stuck sooner or later as your application grows. But I decided to give a chance to this new tool and got really convinced by the tutorials. So we started developing our application with this tool and we have now reached our first release. So let's have a review:

  • The first main question is about resizable layout. You can edit all the required layout information using the composer, but for the moment it's clearly more oriented for static layouts. So we decided to develop a fixed-size application. I hope future releases of JavaFX and of the composer will facilitate the development of resizable applications (for example by providing a resizable scene like the JFxtras' XScene)
  • The composer is designed to work with a single design file, i.e., your whole interface code is stored in a single JavaFX class. We quickly reached a point were we got a "code too large" error, due to the size of the class which is limited by the compiler. At this point I really thought I would stop using the composer, since I couldn't figure out how to split the design file without loosing the advantages of the composer. I have finally found an approach which proved to be quite handy using placeholders panels to integrate the nodes of separate design files. I'll post a separate blog article about this.
  • Another big drawback of the Composer is that you can't work with custom nodes (for examples with JFXtras controls). It would be really interesting to be able to manipulate custom nodes with the composer. For the moment the solution is to use a placeholder and to insert your component manually. As long as you use mostly standard JavaFX components this is not too annoying, but it can be quickly annoying if you work with many custom nodes or controls.
  • The state system is robust and provides a very nice way to manage the view according to the logic of the application. It's very easy and powerful to work with the states. The only point that was sometimes problematic is that you often edit some properties unintentionally in a specific state instead of in the master state. The "set to master" button is then really helpful.
  • Some minor features are still buggy. For example the edition of the Tooltip of a node does not work for the moment, clicking on the add button just lead to some Netbeans errors. However, this can still be done manually.
  • The auto-completion is unfortunately not provided when you edit the code of a binding through the property dialog, that would be handy.
  • Refactoring does not work in the code managed by the composer. So if your gui is bound to a model object and that you refactor the name of this object, the managed code won't be updated and you will have to do it manually through the composer gui.
To conclude I must admit that I've been really impressed by the Composer tool. It's stable and robust. The resulting JavaFX code is clear and remains clean even after hours playing with the composer. Some of the drawbacks and limitations I've enumerated are clearly annoying, but finally the composer is very very promising. I wouldn't have bet one month ago that we would have developed a business application with this tool. Now it's done, and I'm not sure I would be able to give up this composer so easily.

JavaFX chart API and logarithmic scale

 

JavaFX provides an impressive chart API that allow to draw a wide range of charts. Unfortunately the only scale that is available for XY charts is a linear implementation.  Since the javadoc of the API is sometimes a bit rough it took me some times to get figure out how to get a logarithmic scale. After a discussion on mix.oracle.com it turned out that there where to approaches: 

  • the first one is to transform your data with the logarithmic function before injecting it into the chart and to adjust the formatting of the labels to "simulate" a logarithmic scale. Since I don't really like the idea of manipulating the data to get the expected representation I prefer the second approach:
  • the second one is to provide your own implementation of a logarithmic Axis. Let's see how to do this:
Extend the ValueAxis class. There are two methods to implement:
public getDisplayPosition(valuejava.lang.Object) : Number 
 will do the mapping between a data value and its visual position. For a log axis, the implementation will look like this:

protected abstract updateTickMarks() : Void  will define which tick marks should be displayed. The javadoc does not say much about this function. It looks like the main idea is to update the tickMarks sequence of TickMark. The fields of TickMark are weirdly flagged as public-read package, so in oder to instantiate TickMark for our implementation we first need to define a class extending TickMark, let's say CustomTickMark, and to put it in the package javafx.scene.chart.part in order to be able to set the required fields. After that you just need to update the sequence of tick marks with the marks you want to see on your chart, which can look like this:
Well, you're now ready to use this LogAxis with your chart. The following example demonstrate the use of this LogAxis as x-scale of a chart.
Log_scale