Java Reference
In-Depth Information
As you can detect from this code snippet, rounded rectangles are easily created using the arcWidth(double v)
and arcHeight(double v) methods, where the parameter v defines the diameter of the arc.
Dragging the Stage on the Desktop When a Title Bar Isn't Available
The Stage may be dragged on the desktop using its title bar, but in the case where its StageStyle is UNDECORATED or
TRANSPARENT , the title bar isn't available. To allow dragging in this circumstance, we added the code shown in the
following code snippet from Listing 2-1.
//when mouse button is pressed, save the initial position of screen
rootGroup.setOnMousePressed((MouseEvent me) -> {
dragAnchorX = me.getScreenX() - stageRef.getX();
dragAnchorY = me.getScreenY() - stageRef.getY();
});
//when screen is dragged, translate it accordingly
rootGroup.setOnMouseDragged((MouseEvent me) -> {
stageRef.setX(me.getScreenX() - dragAnchorX);
stageRef.setY(me.getScreenY() - dragAnchorY);
});
Event handlers are covered a little later in the chapter, but as a preview, the lambda expression that is supplied to
the onMouseDragged() method is called when the mouse is dragged. As a result, the values of the x and y properties
are altered by the number of pixels that the mouse was dragged, which moves the Stage as the mouse is dragged.
Using UI Layout Containers
When developing applications that will be deployed in a cross-platform environment or are internationalized, it is
good to use layout containers . One advantage of using layout containers is that when the node sizes change, their
visual relationships with each other are predictable. Another advantage is that you don't have to calculate the location
of each node that you place in the UI.
The following snippet from Listing 2-1 shows how the VBox layout class, located in the javafx.scene.layout
package, is used to arrange the Text , CheckBox , HBox , and Button nodes in a column. This snippet also shows that
layout containers may be nested, as demonstrated by the HBox with the name titleBox that arranges the Label and
TextField nodes horizontally. Note that several lines of code are omitted from this snippet to show the layout
nesting clearly:
HBox titleBox = new HBox(titleLabel, titleTextField);
VBox contentBox = new VBox(
textStageX, textStageY, textStageW, textStageH, textStageF,
checkBoxResizable, checkBoxFullScreen,
titleBox, toBackButton, toFrontButton, closeButton);
The VBox layout class is similar to the Group class discussed in the Hello Earthrise example in Chapter 1, in that it
contains a collection of nodes within it. Unlike the Group class, the VBox class arranges its contained nodes vertically,
spacing them apart from each other by the number of pixels specified in the spacing property.
 
Search WWH ::




Custom Search