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
For example, when an I/O error occurs, JPadFX executes new Alert(stage, "I/O error: "+ioe.getMessage()).show(); to notify the user via a dialog box. The primary stage instance is passed as the first argument; an exception message is
passed as the second argument.
The constructor creates a scene consisting of the message and an OK button (whose event handler closes the stage). The javafx.scene.layout.VBoxBuilder class creates a single-column scene consisting of a label containing the message and a button. The scene is centered in the
dialog box.
VBoxBuilder is an example of a builder class that lets you create a container by conveniently chaining method calls together. create() instantiates this class. Remaining method calls specify the builder's children, alignment, padding, and spacing. The final
build() call creates and returns a javafx.scene.layout.VBox instance, which is a layout container that lays out its children in a single vertical column.
Alert() uses the Scene(Parent root) constructor to specify the scene graph's root node. A width and height are not specified because we want the scene's size
to be automatically calculated based on the preferred size of its content.
Window's void sizeToScene() method is called to set the stage window's height/width to match the scene's height/width. Stage's void setResizable(boolean value) method is called with a false argument to prevent the stage from being resized.
The centering calculations for the Alert dialog box rely on scene.getWidth() and scene.getHeight(), which return the scene's width and height. But these method calls return 0.0, because explicit width and height values were
not specified for the scene. To address this problem, we have to briefly show and then hide the dialog box and its scene,
thus prompting scene.getWidth() and scene.getHeight() to return the property values.
Figure 2 shows the Alert dialog box.

The AreYouSure dialog box, which display a message and prompts the user to press a Yes or No button, is implemented by the
AreYouSure class. Listing 5 reveals AreYouSure's source code.
public class AreYouSure extends Stage
{
private EventHandler<ActionEvent> ehaeYes, ehaeNo;
public AreYouSure(Stage owner, String msg)
{
setTitle("Are You Sure?");
initOwner(owner);
initStyle(StageStyle.UTILITY);
initModality(Modality.APPLICATION_MODAL);
Button btnYes = new Button("Yes");
btnYes.setOnAction(new EventHandler<ActionEvent>()
{
@Override
public void handle(ActionEvent ae)
{
if (ehaeYes != null)
ehaeYes.handle(ae);
close();
}
});
Button btnNo = new Button("No");
btnNo.setOnAction(new EventHandler<ActionEvent>()
{
@Override
public void handle(ActionEvent ae)
{
if (ehaeNo != null)
ehaeNo.handle(ae);
close();
}
});
btnNo.setPrefWidth(60.0);
btnYes.setPrefWidth(60.0);
HBoxBuilder hbb;
hbb = HBoxBuilder.create()
.children(btnYes, btnNo)
.spacing(10);
VBoxBuilder vbb;
vbb = VBoxBuilder.create()
.children(new Label(msg), hbb.build())
.padding(new Insets(10.0))
.spacing(10.0)
.alignment(Pos.CENTER);
Scene scene = new Scene(vbb.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);
}
public void setOnYes(EventHandler<ActionEvent> ehae)
{
ehaeYes = ehae;
}
public void setOnNo(EventHandler<ActionEvent> ehae)
{
ehaeNo = ehae;
}
}
AreYouSure's constructor creates a message box that displays a message centered over a pair of Yes and No buttons. The constructor is
largely similar to Alert's constructor, but with three essential differences:
More from JavaWorld