While I was trying JavaFX, I found three main interesting areas:
- Application design approaches: The three main approaches to develop Java-FX based applications.
- Datasets display: The new approach to display data into tables or trees, because this is achieved in a very different way from Swing.
- Multithreading/Hard-tasks-processing: The basics to impement large/hard tasks that must be "observed" from the graphical user interface (using the JavaFX controls).
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:
- Develop your custom JavaFX components.
- Integrate certain-well-tested Swing components.
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:
- The required custom component does not exists on the oficial JavaFX bundles.
- The required custom component does not exists as a JavaFX-third part library or the source code to create it is not available.
- You do not have enough knowledgment or development skills about the JavaFX platform.
- The required custom component exists on Swing and it is well probed that it's performance is correct.
- You have moderated knowledgment and skills on Java Swing.
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:
- http://docs.oracle.com/javase/tutorial/uiswing/concurrency/dispatch.html (English)
- http://chuwiki.chuidiang.org/index.php?title=El_EDT_(Event_Dispatch_Thread)_de_Java (Spanish)
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:
Publicar un comentario