Is my Stage visible ?

In my application, I often show some “popup window” for user’s input.

Quite often, I simply use JavaFX Alert class, and it does the trick :

Alert alert = new Alert(Alert.AlertType.WARNING);
alert.setHeaderText("Be careful");
alert.setContentText("I warned you");

But sometimes, I need to display more complex windows and I also want to remember where the user has left the window. For example, if the user has placed the search window in the upper-left corner. If he closes it and opens it again, maybe he would like to see it appear in the same spot.

That’s very convenient but I saw two problem occurring :

  • The Window was displayed on a second monitor and the user has unplugged it. Now I’m trying to reapply values gathered from cache, and my window is obviously nowhere to be seen.
  • The window was displayed, and it grows in height or width. Then, there’s a chance that the window will go out of screen, and will only be partially visible.

For these two cases, I made a very simple method that will compute whether my current window (Stage) is wrongly displayed :

     * Finds the screen where the Stage is and return true if some parts of the
     * stage are out of the screen, therefore not visible.
     * @param stage
     * @return
    public static boolean isStageOutOfBounds(Stage stage) {
        List<Screen> screenList = Screen.getScreensForRectangle(stage.getX(), stage.getY(), stage.getWidth(), stage.getHeight());
         * If the Stage is overlaping on several screens or not visible on any
         * screens, we assume we can't completly see it.
        if (screenList.size() != 1) {
            return true;
        Rectangle2D bounds = screenList.get(0).getVisualBounds();
        return stage.getX() < bounds.getMinX() 
                || stage.getX() + stage.getWidth() > bounds.getMaxX()
                || stage.getY() < bounds.getMinY() 
                || stage.getY() + stage.getHeight() > bounds.getMaxY();

Then, I only need to make the verification, and call the appropriate :


if necessary.

2 thoughts on “Is my Stage visible ?”

  1. Does your blog have a contact page? I’m having trouble locating it but, I’d like
    to shoot you an e-mail. I’ve got some recommendations
    for your blog you might be interested in hearing.
    Either way, great site and I look forward to seeing it grow over time.

    1. Hi,

      My contact page is my website situated at the top in “about me” :

      You can contact me from here, on my twitter, or directly at samir . hadzic . pro [at] gmail . com

      Looking forward to your recommendations,

Leave a Reply

Your email address will not be published. Required fields are marked *