2

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.

ceklock
  • 6,143
  • 10
  • 56
  • 78

4 Answers4

13

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:

enter image description here

ceklock
  • 6,143
  • 10
  • 56
  • 78
  • I used this code and on button click, I said this `Messages.showPopupMessage("Testing please", new Stage());` Please note `Messages` is my custom alert class. That's said, Nothing was shown on the window. How is this supposed to work and where is the code supposed to be placed? Is my code right? or how do I call the showPopup? – Karue Benson Karue Jun 09 '21 at 18:08
9

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();
user299565
  • 91
  • 1
  • 1
5

Use a ControlsFX dialog or notification pane.

See also How to create and show common dialog (Error, Warning, Confirmation) in JavaFX 2.0?

Community
  • 1
  • 1
jewelsea
  • 150,031
  • 14
  • 366
  • 406
  • Does it work with JavaFX 2? Your links says "ControlsFX is a library of UI controls and useful API for JavaFX 8.0 and beyond". – ceklock Sep 07 '13 at 17:15
  • 1
    That's correct, ControlsFX is a 3rd party library for [Java 8+](https://jdk8.java.net/download.html). For JavaFX 2.x you can use the Stackoverflow answers I linked. – jewelsea Sep 07 '13 at 17:22
  • At the end I will use a Popup. 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. I think my alternative is better if I want to only display a simple message. Would you like me to post the source code of my solution? – ceklock Sep 08 '13 at 01:48
  • (I don't need a dialog in this case, so a popup will do the job). – ceklock Sep 08 '13 at 02:00
  • @ceklok You can provide an answer to your own question with source code and mark it correct if you like. – jewelsea Sep 08 '13 at 03:29
2

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;
}
Paweł
  • 205
  • 2
  • 11