Recommended: Sing it, brah! 5 fabulous songs for developers
JW's Top 5
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
Page 3 of 6
KeyCharacterCombination kccNew;
kccNew = new KeyCharacterCombination("N", KeyCombination.CONTROL_DOWN);
miNew.setAccelerator(kccNew);
More differences between JavaFX and Swing emerge when we start adding menu items to a menu, and menus to a menu bar. Whereas
Swing's JMenu and JMenuBar classes provide various add() methods for this task, JavaFX's Menu and MenuBar classes are not as direct.
JavaFX's Menu and MenuBar store their respective MenuItem and Menu instances in observable lists, which are java.util.List<E> implementations that let listeners track changes as they occur, such as an item being added to or removed from a List.
Observable lists are returned by calling Menu's ObservableList<MenuItem> getItems() and MenuBar's ObservableList<Menu> getMenus() methods. After obtaining the observable list, the Menu or MenuItem instance will be added by calling the list's add() method.
Observable lists are used with JavaFX's binding feature for keeping parts of a UI in sync with other parts of the UI or a
data model. For example, Menu stores its menu items in an observable list so that it can dynamically update the menu whenever it's notified that the list
has changed at runtime.
After creating and configuring the menu bar, start() adds this control to the top portion of the borderpane by calling BorderPane's void setTop(Node n) method. Similar void setCenter(Node n) and void setBottom(Node n) methods are called to add newly created javafx.scene.control.TextArea and javafx.scene.control.Label instances to the borderpane.
JavaFX provides the javafx.scene.Scene class to serve as a container for a scene graph. Each of this class's constructors requires that its first argument be a
reference to the node that serves as the scene graph's root, which happens to be the BorderPane instance. start() invokes the Scene(Parent root, double width, double height, Paint fill) constructor class to store this root node along with scene dimensions and a background color:
javafx.scene.Parent is the base class for those nodes that have child nodes.
javafx.scene.paint.Paint is the base class for a color or gradient used to fill shapes and backgrounds when rendering a scene.
In JavaFX, scenes are managed by stages. start() assigns the Scene instance to the primaryStage instance by invoking Stage's void setScene(Scene scene) method. It then assigns a title to the stage by invoking Stage's void setTitle(String title) method.
Finally, start() displays the primary stage along with its scene by invoking Stage's void show() method.
You can think of the primary stage as JavaFX's counterpart to a Swing application's frame window. Additional stages can serve as dialog boxes, which I discuss in Part 3. The scene that is added to the primary stage is JavaFX's counterpart to Swing's content pane.
We've looked at some of the differences between JavaFX and Swing, primarily in a menu system context. Now we'll conclude Part 2 by considering something more significant, how JavaFX handles events. This will be an overview based on the limited features of the JPad demo application. See Resources for a more in-depth discussion of JavaFX 2's event handling framework.
More from JavaWorld