Java Reference
In-Depth Information
return;
}
double result = calculateBMI(mass, height);
showMessage("Your BMI is: " +
(Math.round(result*100.0)/100.0), "Your BMI result");
}
});
Scene scene = new Scene(new Group(vbox));
stage.setTitle("JavaFX BMI Calculator");
stage.setScene(scene);
stage.sizeToScene();
stage.show();
}
protected double calculateBMI(double mass, double height) {
return mass / Math.pow(height/100.0, 2.0);
}
public static void main(String[] args) {
Application.launch(args);
}
public void showMessage(final String message, final String title) {
final Stage dialog = new Stage(StageStyle.UTILITY);
dialog.setTitle(title);
dialog.setResizable(false);
dialog.initModality(Modality.WINDOW_MODAL);
dialog.initOwner(this.stage);
VBox vbox = new VBox(2);
HBox pane = new HBox(10);
dialog.setScene(new Scene(vbox));
vbox.setAlignment(Pos.CENTER);
vbox.getChildren().add(pane);
pane.getChildren().add(new Label(message));
Button btn = new Button("OK");
btn.setOnAction(new EventHandler<ActionEvent>() {
@Override
public void handle(ActionEvent e) {
dialog.close();
}
});
pane.getChildren().add(btn);
dialog.showAndWait();
}
}
Note that JavaFX does not provide a builtā€in method to show dialogs (this might be included in
future Java updates, though). Here, a simple showMessage method was added that constructs a new
stage window to show a message with an OK button. The end result looks like Figure 11-34.
Search WWH ::




Custom Search