Newsletter sign-up
View all newsletters

Enterprise Java Newsletter
Stay up to date on the latest tutorials and Java community news posted on JavaWorld

Sponsored Links

Optimize with a SATA RAID Storage Solution
Range of capacities as low as $1250 per TB. Ideal if you currently rely on servers/disks/JBODs

Practical JavaFX 2, Part 3: Refactoring Swing JPad's advanced UI features

Migrate Swing JPad's dialogs, clipboard, and drag-and-drop to JavaFX

  • Print
  • Feedback

Page 6 of 6

If files have been dragged, DragEvent's void acceptTransferModes(TransferMode... transferModes) method is called to tell the drag-and-drop runtime that the text area lets files be dropped onto it, and signifies to the user that it's interpreting the drag as a copy operation.

Next, we create a DragEvent-parameterized EventHandler instance that responds to "drag dropped" drag events. It registers this instance with the TextArea by invoking void setOnDragDropped(EventHandler<? super DragEvent> value).

The event-handler first obtains the Dragboard instance and then verifies that the dragboard contains a list of files. If the text area's contents are dirty, the user is prompted to save changes, after which changes are saved. After invoking DragBoard's List<File> getFiles() method to retrieve the list of files, the handler opens the first file and then invokes DragEvent's void setDropCompleted(boolean isTransferDone) method to indicate transfer success or failure.

Conclusion to Part 3

Part 1's introduction presented the following two questions for you to ponder while reading this article:

  1. Is JavaFX 2 a production-ready platform, as Oracle claims?
  2. Are there notable advantages to programming in JavaFX versus Swing?

For many applications, JavaFX 2 is already production-ready. However, this did not prove true for JPadFX, for the following reasons:

  • JavaFX 2 does not support a native undo capability. In order to include this feature in JPadFX, I would have had to rely on Swing, which I chose not to do. Similarly, JavaFX 2 does not support a native printing capability. Once again, I would need to rely on Swing for assistance to add print support to JPadFX. (I didn't support printing with Swing JPad, but I could have done so.)
  • JavaFX 2 does't support common dialog boxes for alerts, confirmations, and so on. I had to code my own AreYouSure and Alert dialogs.
  • JavaFX 2 is inconsistent with thread execution whenever a file-chooser modal dialog box or a stage-based modal dialog box is presented. Regarding the file chooser, further execution is suspended until the dialog box has been dismissed. Regarding a stage-based modal dialog box, execution continues while the dialog box is shown. This behavior necessitates the adoption of a cumbersome callback model to delay further UI execution until the dialog stage is closed.

These examples demonstrate that JavaFX 2 still lacks important features, and is occasionally cumbersome to use. That said, in answer to the second question, yes: I believe that there are notable advantages to programming in JavaFX. Consider these advantages:

  • JavaFX 2 provides a rich assortment of features, including animation and effects. Although I didn't utilize animation for JPadFX, I relied on its reflection effect to create an interesting About dialog box. If I wanted to use reflection in Swing JPad's About dialog box, I would have had to create a reflected image of the dialog box's content, and then display this image. This technique would not be possible if I later needed to animate the About box's content and reflect the animated content.
  • JavaFX 2 provides a simpler and more consistent event-handling framework than the Swing equivalent (which is based on the Abstract Window Toolkit's event-handling framework).
  • JavaFX 2 provides a unified architecture for writing an application once and then deploying it to various contexts (e.g., embedded in a web browser or run via Java Web Start). Additional contexts will present themselves in the future (e.g., running the same application on a mobile device). To help the application determine its execution context, JavaFX 2 provides host services. As you learned, this feature helps an application adapt to its environment (e.g., JPadFX does not display an Exit menu when it discovers that it is running in a web page -- Exit makes no sense in this context.

Just as I could have expanded the previous list, I could also add more items to this list. The point here is that JavaFX 2 does offer many compelling features that are worth investigating and using.

What do you think?

Is JavaFX 2 ready for production? Are you willing to live with the trade-offs in order to begin writing JavaFX applications now? Share your thoughts in the Comments section of this article.

I think that JavaFX 2's advantages outweigh its current limitations, and I encourage you to consider migrating some of your Swing UIs to JavaFX. Keep in mind that you may have to fall back on Swing for features that are currently not supported by JavaFX 2. However, the need to do so will diminish as JavaFX's feature set expands. Although JavaFX 2's threading model results in more cumbersome code to properly deal with modal stage-based dialog boxes, perhaps this limitation will also be addressed in a future JavaFX release.

About the author

Jeff Friesen is a freelance tutor and software developer with an emphasis on Java and Android. In addition to writing Java and Android books for Apress, Jeff has written numerous articles on Java and other technologies for JavaWorld, informIT, Java.net, and DevSource. Jeff can be contacted via his website at TutorTutor.ca.

Read more about Enterprise Java in JavaWorld's Enterprise Java section.

  • Print
  • Feedback

Resources

More from JavaWorld

  • Find more of Jeff's writing in Java Tutor, his blog on JavaWorld.
  • See the JavaWorld Site Map for a complete listing of research centers focused on client-side, enterprise, and core Java development tools and topics.
  • JavaWorld's Java Technology Insider is a podcast series that lets you learn from Java technology experts on your way to work.