JavaFX: can you create a stage that doesn't show on the task bar and is undecorated?

前端 未结 6 810
予麋鹿
予麋鹿 2020-12-15 21:55

I\'m trying to create a stage that doesn\'t appear on the Windows task bar and is undecorated (no borders and no close/minimize/maximize buttons). My end goal is to create a

6条回答
  •  挽巷
    挽巷 (楼主)
    2020-12-15 22:30

    This question is not really fresh, but I had the same problem and found no solution at first, but here is my workaround :)

    The best solution I found was to set the primaryStage to style Utility and to make all childStages to Style Undecorated. Then set the opacity of the primaryStage to 0.0 so its not visible: PrimaryStage:

    primaryStage.initStyle(StageStyle.UTILITY);
    primaryStage.setOpacity(0);
    primaryStage.show();
    

    Childs:

    this.initOwner(owner);
    this.initStyle(StageStyle.UNDECORATED);
    

    Example code

    public void start(Stage primaryStage) throws IOException{
        //make primaryStage utility
        primaryStage.initStyle(StageStyle.UTILITY);
        primaryStage.setOpacity(0);
    
        Stage secondaryStage = new Stage();
        secondaryStage.initOwner(primaryStage);
        //make secondaryStage transparent
        secondaryStage.initStyle(StageStyle.TRANSPARENT);
    
    
        //load ui via FXMLLoader
        FXMLLoader loader = new FXMLLoader(getClass().getResource("/ui.fxml"));
        AnchorPane pane = loader.load();//example with anchor pane
    
        Scene scene = new Scene(pane,400,400); //example width and height
        scene.setFill(Color.TRANSPARENT); //make scene transparent as well
    
        secondaryStage.setScene(scene);
    
        primaryStage.show();
        secondaryStage.show();
    }
    

提交回复
热议问题