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:
Collected from the Internet
Please contact [email protected] to delete if infringement.
Comments