|
|
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 2 of 6
Before ImageView is instantiated, Image is instantiated and told to load contents of image.png. Because this PNG file will be stored in a JAR file, it's accessed via the expression getClass().getResourceAsStream("icon.png").
Next, the ImageView node's width and height are obtained to help position the scene. This node is positioned 10 pixels from the left edge of
the dialog box and centered vertically. It is then added to the group's observable list. (See Part 2 for a discussion about observable lists.)
The javafx.scene.text package's Text and Font classes are instantiated to describe two text nodes that are displayed with the image. The text is colored white to contrast
with the scene's black background. The font is assigned and these nodes are added to the group's observable list.
JavaFX lets you add one or more effects to a node. For example, javafx.scene.effect.Reflection is used to reflect a node. About's constructor instantiates Reflection and calls its void setFraction(double value) method, specifying that all of the image must be visible. The constructor also calls javafx.scene.Node's void setEffect(Effect effect) method on the group node to reflect this node's contents.
The scene is now created and an event handler is registered to respond to mouse-pressed events. When the mouse is pressed
over the About stage, Stage's void close() method will be called to close the stage.
After assigning the scene to the stage, there's just one thing left to do. Centering the About stage over the primary stage
will add a touch of professionalism to our UI. We do this by calling Stage's setX() and setY() property setter methods with the results of our centering calculations, as shown in the final two lines of Listing 3.
Figure 1 shows the About dialog box.

The Alert dialog box, which displays messages resulting from I/O exceptions, is implemented by the Alert class. Listing 4 reveals Alert's source code.
public class Alert extends Stage
{
public Alert(Stage owner, String msg)
{
setTitle("Alert");
initOwner(owner);
initStyle(StageStyle.UTILITY);
initModality(Modality.APPLICATION_MODAL);
Button btnOk = new Button("Ok");
btnOk.setOnAction(new EventHandler<ActionEvent>()
{
@Override
public void handle(ActionEvent ae)
{
close();
}
});
final Scene scene = new Scene(VBoxBuilder.create()
.children(new Label(msg),
btnOk)
.alignment(Pos.CENTER)
.padding(new Insets(10))
.spacing(20.0)
.build());
setScene(scene);
sizeToScene();
setResizable(false);
show(); hide(); // needed to get proper value from scene.getWidth() and
// scene.getHeight()
setX(owner.getX()+Math.abs(owner.getWidth()-scene.getWidth())/2.0);
setY(owner.getY()+Math.abs(owner.getHeight()-scene.getHeight())/2.0);
}
}
Alert's constructor takes a Stage argument identifying the stage that owns the Alert dialog box. It also takes a String argument that describes the message to be shown. JPadFX passes the primary stage instance and a suitable message to this
constructor.
More from JavaWorld