In my application I need to display a warning/info message, but I don't know a simple way to do that because there is no JOptionPane or similar component on JavaFX.
There is a Popup class, but you have to set many parameters to get a decent layout/position/background color/etc for a simple message... So I want to know if there is a simple, already implemented component to display messages, maybe in a third party library if JavaFX does not offer anything decent.
Thanks in advance for any help/hints.
ControlsFX is JavaFX 8 so I can't use it, and the other alternatives are almost the same level of complexity of what I was going to do. So I implemented my own methods to display an info/warning message using a Popup.
Here is the source code of what I implemented. Just some tweaks to show the Popup centered on the window, use CSS and hide when the user clicks on it.
public static Popup createPopup(final String message) {
final Popup popup = new Popup();
popup.setAutoFix(true);
popup.setAutoHide(true);
popup.setHideOnEscape(true);
Label label = new Label(message);
label.setOnMouseReleased(new EventHandler<MouseEvent>() {
@Override
public void handle(MouseEvent e) {
popup.hide();
}
});
label.getStylesheets().add("/css/styles.css");
label.getStyleClass().add("popup");
popup.getContent().add(label);
return popup;
}
public static void showPopupMessage(final String message, final Stage stage) {
final Popup popup = createPopup(message);
popup.setOnShown(new EventHandler<WindowEvent>() {
@Override
public void handle(WindowEvent e) {
popup.setX(stage.getX() + stage.getWidth()/2 - popup.getWidth()/2);
popup.setY(stage.getY() + stage.getHeight()/2 - popup.getHeight()/2);
}
});
popup.show(stage);
}
CSS:
.popup {
-fx-background-color: cornsilk;
-fx-padding: 10;
-fx-border-color: black;
-fx-border-width: 5;
-fx-font-size: 16;
}
The pop-up message:
Use a ControlsFX dialog or notification pane.
See also How to create and show common dialog (Error, Warning, Confirmation) in JavaFX 2.0?
With Java 8u40 and upwards, you don't need to worry. It has JavaFX Dialogs.
Here is a Link to Learn: http://code.makery.ch/blog/javafx-dialogs-official/
Just as simple as:
Alert alert = new Alert(AlertType.INFORMATION);
alert.setTitle("Information Dialog");
alert.setHeaderText("Look, an Information Dialog");
alert.setContentText("I have a great message for you!");
alert.showAndWait();
Small improvement of ceklock answer
added:
-can't dimiss by click
-auto hide
-change popup position to bottom of window
-you dont have to looking for stage in file, you can pass any control view (button, label, container or what do you want)
JUST CALL:
Toast.show("YEEEEEEEEEEE", anyControl);
import javafx.animation.KeyFrame;
import javafx.animation.Timeline;
import javafx.scene.control.Control;
import javafx.scene.control.Label;
import javafx.stage.Popup;
import javafx.stage.Stage;
import javafx.util.Duration;
public class Toast {
private static int TOAST_TIMEOUT = 1400;
private static Popup createPopup(final String message) {
final Popup popup = new Popup();
popup.setAutoFix(true);
Label label = new Label(message);
label.getStylesheets().add("/css/mainStyles.css");
label.getStyleClass().add("popup");
popup.getContent().add(label);
return popup;
}
public static void show(final String message, final Control control) {
Stage stage = (Stage) control.getScene().getWindow();
final Popup popup = createPopup(message);
popup.setOnShown(e -> {
popup.setX(stage.getX() + stage.getWidth() / 2 - popup.getWidth() / 2);
popup.setY(stage.getY() + stage.getHeight() / 1.2 - popup.getHeight() / 2);
});
popup.show(stage);
new Timeline(new KeyFrame(
Duration.millis(TOAST_TIMEOUT),
ae -> popup.hide())).play();
}
css:
.popup {
-fx-background-color: cornsilk;
-fx-padding: 10;
-fx-border-color: black;
-fx-border-width: 5;
-fx-font-size: 16;
}