JavaFX

From APIDesign

(Difference between revisions)
Jump to: navigation, search
(Dispatch Threads)
(Dispatch Threads)
Line 3: Line 3:
=== Dispatch Threads ===
=== Dispatch Threads ===
-
Up until today (e.g. May 2012) mixing [[Swing]] and [[JavaFX]] was not easy because each of these two systems used its own dispatch thread. Whenever one needed to operate with [[Swing]] one had to enter the dispatch thread by using {{JDK|java/awt|EventQueue}}.invokeLater({{JDK|java/lang|Runnable}}).
+
Up until today (e.g. May 2012) mixing [[Swing]] and [[JavaFX]] was not easy because each of these two systems used its own dispatch thread. Whenever one needed to operate with [[Swing]] one had to enter the dispatch thread by using {{JDK|java/awt|EventQueue}}.invokeLater({{JDK|java/lang|Runnable}}). As soon as there was a need to manipulate with [[JavaFX]] components one had to use {{FX|javafx/application|Platform}}.runLater({{JDK|java/lang|Runnable}}). As [[Java]] is not very good at this kind of actor based coding, the result was completely unreadable code full of deeply nested {{JDK|java/lang|Runnable}}s:
 +
 
 +
<source lang="java">
 +
public static void main(String[] args) {
 +
SwingUtilities.invokeLater(new Runnable() {
 +
@Override
 +
public void run() {
 +
JFrame frame = new JFrame("FX");
 +
final JFXPanel fxPanel = new JFXPanel();
 +
frame.add(fxPanel);
 +
frame.setVisible(true);
 +
 
 +
Platform.runLater(new Runnable() {
 +
@Override
 +
public void run() {
 +
// This method is invoked on JavaFX thread
 +
Scene scene = createScene();
 +
fxPanel.setScene(scene);
 +
}
 +
});
 +
}
 +
});
 +
}
 +
</source>

Revision as of 08:27, 23 May 2012

JavaFX is new, modern UI toolkit for rendering Java UIs. The idea is tempting - instead of bloated AWT and Swing on top, one can use a completely independent UI and lightweight library instead. However, as usual when offering AlternativeBehaviour, the problem is co-existence - there a tons of Swing applications around and they need to migrate to JavaFX in an incremental fashion. Unless such migration is smooth, people will rather stick with what they have right now.

Dispatch Threads

Up until today (e.g. May 2012) mixing Swing and JavaFX was not easy because each of these two systems used its own dispatch thread. Whenever one needed to operate with Swing one had to enter the dispatch thread by using EventQueue.invokeLater(Runnable). As soon as there was a need to manipulate with JavaFX components one had to use Platform.runLater(Runnable). As Java is not very good at this kind of actor based coding, the result was completely unreadable code full of deeply nested Runnables:

public static void main(String[] args) {
    SwingUtilities.invokeLater(new Runnable() {
        @Override
        public void run() {
             JFrame frame = new JFrame("FX");
             final JFXPanel fxPanel = new JFXPanel();
             frame.add(fxPanel);
             frame.setVisible(true);
 
             Platform.runLater(new Runnable() {
                 @Override
                 public void run() {
                     // This method is invoked on JavaFX thread
                     Scene scene = createScene();
                     fxPanel.setScene(scene);
                 }
             });
        }
    });
}
Personal tools
buy