Skip to content

Instantly share code, notes, and snippets.

@alvareztech
Last active May 3, 2017 17:57
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save alvareztech/b51b197123172b0e71a493efe8afebc1 to your computer and use it in GitHub Desktop.
Save alvareztech/b51b197123172b0e71a493efe8afebc1 to your computer and use it in GitHub Desktop.
JavaFX: Alerts
import javafx.application.Application;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.geometry.Insets;
import javafx.scene.Scene;
import javafx.scene.control.Alert;
import javafx.scene.control.Button;
import javafx.scene.layout.VBox;
import javafx.stage.Stage;
public class Main extends Application {
public static void main(String[] args) {
launch(args);
}
@Override
public void start(Stage primaryStage) throws Exception {
Button boton1 = new Button("Alerta 1");
Button boton2 = new Button("Alerta 2");
Button boton3 = new Button("Alerta 3");
boton1.setOnAction(new EventHandler<ActionEvent>() {
@Override
public void handle(ActionEvent event) {
Alert alert = new Alert(Alert.AlertType.INFORMATION);
alert.setTitle("Este es un díalogo de información");
alert.setHeaderText("Este es el mensaje de cabecera");
alert.setContentText("Aquí una descripción más larga");
alert.showAndWait();
}
});
boton2.setOnAction(new EventHandler<ActionEvent>() {
@Override
public void handle(ActionEvent event) {
Alert alert = new Alert(Alert.AlertType.INFORMATION);
alert.setTitle("Este es un díalogo sin cabecera");
alert.setHeaderText(null);
alert.setContentText("Aquí una descripción más larga");
alert.showAndWait();
}
});
boton3.setOnAction(new EventHandler<ActionEvent>() {
@Override
public void handle(ActionEvent event) {
Alert alert = new Alert(Alert.AlertType.ERROR);
alert.setTitle("Este es un díalogo de error");
alert.setHeaderText("Este es el mensaje de cabecera");
alert.setContentText("Aquí una descripción más larga");
alert.showAndWait();
}
});
VBox vBox = new VBox();
vBox.getChildren().add(boton1);
vBox.getChildren().add(boton2);
vBox.getChildren().add(boton3);
vBox.setSpacing(20);
vBox.setPadding(new Insets(20, 20, 20, 20));
Scene scene = new Scene(vBox, 400, 400);
primaryStage.setScene(scene);
primaryStage.show();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment