sábado, 27 de septiembre de 2014

Java 8 and the Two Worlds for Desktop-Based Applications: JavaFX - Swing

On last days I was trying and playing seriously with JavaFX 2 (also called only Java FX) because I'm really interested in how to get the maximum advantages for new apps on this platform.

While I was trying JavaFX, I found three main interesting areas:
  1. Application design approaches: The three main approaches to develop Java-FX based applications.
  2. Datasets display: The new approach to display data into tables or trees, because this is achieved in a very different way from Swing.
  3. Multithreading/Hard-tasks-processing: The basics to impement large/hard tasks that must be "observed" from the graphical user interface (using the JavaFX controls).
On next posts I will explain in detail all the above points.

Today, a considerable information amount about the new JavaFX exists on the web, including the official tutorials and documents from Oracle.

There is not doubts about JavaFX is the best adoption choice for new desktop-based developments under the Java Platform. From my point of view, Java FX is amazing and today it is the oficially recommended  platform by Oracle for new desktop-based applications. But... wait... this is not time to forget or isolate it from his ancestor: Swing.

Although JavaFX offers a better user experience that Swing does, it has a lack of custom controls that are necessary in many applications. At this moment, I could identify a couple of them: docking frameworks and rich text editors. This is normal in all new platforms. More custom and advanced components will be released over the time. But, what can you do if you need that components for your today projects? Well... the answer is very simple and you have to choose one of two ways:
  1. Develop your custom JavaFX components.
  2. Integrate certain-well-tested Swing components.
Both approaches have advantages and disadvantages. For example, taking the first way means that you don't "get out" of the JavaFX world. However, the required effort and time to achieve the goal could be expensive. On the other way, if the desired component exists (this is highly probably) in Swing, you can consider to use it into your JavaFX application. But, you must be careful and take into account that you are mixing two different worlds: The Swing World and the JavaFX World. This is not imposible and with appropiate design approaches you can achieve the goal with success.

Choosing one approach is a trade-off between time-effort-skills-platform constraints. But, in many cases, you could take the second way under this conditions:
  1. The required custom component does not exists on the oficial JavaFX bundles.
  2. The required custom component does not exists as a JavaFX-third part library or the source code to create it is not available.
  3. You do not have enough knowledgment or development skills about the JavaFX platform.
  4. The required custom component exists on Swing and it is well probed that it's performance is correct.
  5. You have moderated knowledgment and skills on Java Swing.
In order to ilustrate this approach I will use an application that I developed first on Swing and recently in JavaFX: JGreep / JGreepFX.

JGreep is an old program that I developed on top of the Swing framework some years ago. It's a very simple word-finder that acts over plain text documents. In order to use it, you start specifying a directory. Then, all files contained into the directory and subdirectories are listed into a table (a JTable component). Next, you type a word and the application starts to search the word occurrence inside of the listed text type files.

Next table shows you the JGreep/JgreepFX application, one using Swing and the other using JavaFX:

JGreep JGreepFX
Main Application Window:
Listing files process:


Search process:


Search results:



As you can see in the above pictures, the same application was builded on top of Swing and on top of JavaFX. The look and feel used in Swing is the PGS Look And Feel from Pagosoft.

At this point, you can appreciate some interesting appearance features on both applications. Now, lets to see another feature of the original JGreep: Open and display plain text files with a list of line numbers where the word occurences were found. The next picture shows a screenshot of a plain text file loaded with JGreep:


The JGreep's text viewer displays the file's contents in rich text. This is achieved using the amazing RSyntaxtTextArea Swing component from fifesoft (http://fifesoft.com/rsyntaxtextarea/). But... for JavaFX, today is not a complete rich text editor. Searching in google I found only an early development intended to develop this component: http://github.com/TomasMikula/RichTextFX. However, at this moment, only is available the basic framework to support regexp and you must do by hand the rest of the work.

Then, my approach to have a rich text editor in JGreepFX was using the Swing-based RSyntaxtTextArea. The result is:


The obtained result was very good and the appearance between the two frameworks looks similar. I used an additional trick: The document viewer in JGreepFX uses the Weblaf Look and Feel.This look and feel is very close (but not equals) in appearance to the standard Modena theme in JavaFX.

The most important when you are mixing JavaFX and Swing is: "Never perform tasks of each other out of their own thread". Remember: Tasks related with Swing controls are performed under the Swing's Event Dispatch Thread or EDT, while tasks related with JavaFX controls are performed under the JavaFX's Platform Application Thread.

If you want learn more about Swing's EDT I reommend this links:
  1. http://docs.oracle.com/javase/tutorial/uiswing/concurrency/dispatch.html (English)
  2. http://chuwiki.chuidiang.org/index.php?title=El_EDT_(Event_Dispatch_Thread)_de_Java (Spanish)
If you want learn more about the JavaFX PAT I recommend you to start with the Oracle's docs: http://docs.oracle.com/javafx/2/api/javafx/application/Platform.html.

And, this document about integrating JavaFX and Swing is a "must-read" to successfull achieve the goal: http://docs.oracle.com/javafx/2/swing/swing-fx-interoperability.htm.

To see this concepts in action, you can download the JGreep and JGreepFX, including their source codes from this locations:

http://sourceforge.net/projects/jgreep/

http://sourceforge.net/projects/jgreepfx/

Download the codes and start playing with it. I recommend you to use a Java versión 1.8.20+ because earlier version has a lack of bugs and the JavaFX applications throws a lot of exceptions, specially when you use the TableView control.

Para leer una versión en español de este post, ve a este enlace.

+ Miguel Angel

No hay comentarios: