Reputation: 47
I'm working with the new JavaFX Alert class (Java 1.8_40) and trying to use HTML tags inside the exibition text, but no success so far. Here's an example of what I'm trying to do.
Alert alert = new Alert(AlertType.INFORMATION);
alert.setHeaderText("This is an alert!");
alert.setContentText("<html>Pay attention, there are <b>HTML</b> tags, here.</html>");
alert.showAndWait();
Would anyone know if it is really possible, and show me an example?
Thanks in advance.
Upvotes: 4
Views: 3775
Reputation: 209358
I haven't worked with the new Alert
class much, but I am pretty sure the text properties do not support HTML formatting.
You could use a web view to display HTML-formatted text:
import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.control.Alert;
import javafx.scene.control.Alert.AlertType;
import javafx.scene.control.Button;
import javafx.scene.layout.StackPane;
import javafx.scene.web.WebView;
import javafx.stage.Stage;
public class AlertHTMLTest extends Application {
@Override
public void start(Stage primaryStage) {
Button button = new Button("Show Alert");
button.setOnAction(e -> {
Alert alert = new Alert(AlertType.INFORMATION);
alert.setHeaderText("This is an alert!");
WebView webView = new WebView();
webView.getEngine().loadContent("<html>Pay attention, there are <b>HTML</b> tags, here.</html>");
webView.setPrefSize(150, 60);
alert.getDialogPane().setContent(webView);;
alert.showAndWait();
});
StackPane root = new StackPane(button);
Scene scene = new Scene(root, 350, 75);
primaryStage.setScene(scene);
primaryStage.show();
}
public static void main(String[] args) {
launch(args);
}
}
Upvotes: 5