Blog
JavaFX 2.0 beta
Last weekend the public beta of JavaFX 2.0 came out. I've much anticipated this release, as you might guess from my previous posting on JavaFX 2.0. I've downloaded the JavaFX-runtime, SDK and Netbeans-plugins the following evening from Oracle's JavaFX page and started trying out JavaFX by viewing and running the examples from the SDK from Netbeans. I'm quite enthusiastic, read on the learn more!
JavaFX-overview The new JavaFX is a library, as written in my previous posting. The SDK now just consists of documentation (including examples), a jar file and the JavaFX-plugin. You'll have to install the plugin on your computer before you can use JavaFX, but eventually (probably around Java 7 or 8 ) JavaFX will be included in the default Java-runtime. Currently, only the Windows versions of the plugin and sdk are ready and available. The Windows version uses DirectX, while Mac and Linux versions use OpenGL as shown in the figure below (from taken from JavaFX Architecture and Framework): Figure 1 JavaFX Architecture Diagram
- You'll have to worry less about threads as you had to when using Swing: The event-handling thread is separate from the UI-handling thread. There are three Threads in JavaFX doing their work: Event Dispatch Thread - for handling events, the Renderer thread for rendering graphics and the Media thread for playback of media.
Using multiple threads allows JavaFX to make better use of now common multi-core processors.
However, if you have a lengthy operation inside the event handling thread, using a separate thread is still necessary:
... // create content imageView = new ImageView(); imageView.setImage(new Image(imageUrl, 241, 91, true, true, false)); Button btn = new Button("Press me"); btn.setOnAction(new EventHandler() { public void handle(ActionEvent ke) { //UI will freeze as no events are handled, but at least no repaint //mess as in Swing Thread.sleep(1500); //InterupptedException handling omitted //Rendering is done in separate thread logo.imageView.setEffect(dropShadow); logo.setLayoutX(190); logo.setLayoutY(180); } });
- JavaFX includes ways to very easily apply transitions and various effects. In the example below I add the reflection effect to an existing image object.
Reflection reflection = new Reflection(); reflection.setFraction(REFLECTION_SIZE); imageView.setEffect(reflection);
- Easy property-binding. All properties of JavaFx-controls bindable. This means the values of two properties can be bound together, if one changes, the other one changes as well. You can for example bind the x-property of one control to the x-property of another control. Yet again something which makes a lot of coding easier. In the example below the property of bound the x and y property of a circle object to the logo object:
circle.centerXProperty().bind(logo.layoutXProperty()); circle.centerYProperty().bind(logo.layoutYProperty());
If the logo moves, or more exactly said, if the x or y property changes, so will the circle. The binding is one-way in this case. If I'd use the method bindBidirectional, updates would go the other way around too. You can use expressions in bindings too:circle.centerXProperty().bind(logo.translateXProperty().add(10d));
- Animation and other time-depending effects can be crafted using the Timeline-object. If something has to change over time, the Timeline works a lot easier than using threads, runnables, jobs or other hard to use concepts. In the example below I use the timeline to increase the radiusProperty of a dropshadow. The value is increased linearly from the start-value (in this case 0) up until 20 within a time-frame of 4 seconds. Again, no explicit thread-coding is needed, no need to set locks, use volatile or synchronized keyword or to do other cumbersome stuff.
KeyValue keyValue; KeyValue keyValue = new KeyValue(dropShadow.radiusProperty(), 20d, Interpolator.LINEAR); //create a keyFrame with duration 4s keyFrame = new KeyFrame(Duration.valueOf(4000), keyValue); //add the keyframe to the timeline timeline.getKeyFrames().add(keyFrame); logo.imageView.setEffect(dropShadow); timeline.play()
- Lots of standard-controls, amongst others for tables, buttons, trees and various charts. To quickly get an overview, run the Ensemble project. This project also includes very good documentation to get you started on JavaFX.
- Swing and JavaFX can coexist. You can use JavaFX controls within Swing-applications. If you have a lot of Swing-based Java code, this can be a great plus. Maybe your Swing application does not even need to become a legacy application after all:
JTable table = new JTable(tableModel); JTableHeader header = table.getTableHeader(); header.addMouseListener(new SampleTableSorter(table)); JScrollPane panel = new JScrollPane(table); panel.setPreferredSize(new Dimension(550,100)); frame.getContentPane().add(panel, BorderLayout.SOUTH); // create javafx panel javafxPanel = new JFXPanel(); javafxPanel.setPreferredSize(new Dimension(550,400)); frame.getContentPane().add(javafxPanel, BorderLayout.CENTER); // create JavaFX scene Platform.runLater(new Runnable() { public void run() { createScene(); } }); // show frame frame.pack();
- Last but not least, JavaFX can be used with every language for the JVM! You're not forced to write in Java, or Javascript for that matter. You can create JavaFX-application using JRuby, Scala or Clojure. I think quite a few of the features of JavaFX would fit very well in a (hybrid) functional language.
If you want create rich internet application and you don't want to code in Javascript or Actionscript, the JavaFX-framework helps you out.Here's an example taken from JavaFX-site. If I have some time left I'll create an example in Scala.
$time = Timeline.new $time.cycleCount = Timeline::INDEFINITE $time.keyFrames << KeyFrame.new(Duration.valueOf(1000), refresh_time_handler) sleep(1 - (System.currentTimeMillis()%1000)/1000.0) $time.play()
- Browser integration - JavaFX applications run inside a browser, using the Java-plugin. The Java-plugin will be updated so the JavaFX-runtime is included, when JavaFX is no longer beta. Oracle invest time to make the installation and update of the Java-plugin even more easier. Furthermore most new desktop-plugins have the Java-plugin preinstalled. If Oracle makes the installation and update of the Java-plugin as easy as that of the Flash-plugin then Java on the client might return. The advantage of not having to test the application for every kind of webbrowser (since they all use the same plugin) could outweigh the disadvantage of having to use a plugin.
Gerard van de Glind
Contact
Let’s discuss how we can support your journey.