Wednesday, December 13, 2017

JAVAFX - How show base info dialog (Alert class)

Since java version 8u40 is available javafx.scene.control.Alert for easy info/warning/error dialog.

1) Base form:
import javafx.scene.control.Alert;
import javafx.scene.control.Alert.AlertType;
import javafx.scene.control.ButtonType;
...
Alert alert = new Alert( AlertType.INFORMATION, "This is alert." );
alert.showAndWait();










2) You can specify button (or more buttons):
alert = new Alert( AlertType.INFORMATION, "This is alert.\n\nSecond line.", 
                   new ButtonType( "Close it" ) );
alert.showAndWait();












3) Or you can specify dialog title and remove header text:
Alert alert = new Alert( AlertType.INFORMATION );
alert.setTitle( "Info" );
alert.setHeaderText( null );
alert.setContentText( "This is alert.\n\nSecond line." );
alert.showAndWait();


No comments:

Post a Comment