java - 使用场景/舞台作为按钮打开另一个场景/舞台

标签 java javafx

我在解决这个问题时遇到了困难,并且我找不到我遇到的这个问题的正确答案。我希望将一个场景/阶段用作打开另一个场景/阶段的按钮,并且两者都通过相同的方法调用。完整的方法如下:

 public void createChatWindowMinimized(int agentKey, String message)
    {            
        LOGGER.enterMethod();

        try{                
            ChatAgentsController chatController = null;

            User agent = getModel().getRealtimeAgentNode().getUser(agentKey);
            String firstNameLastName = agent.getFirstname() + " " + agent.getLastName();

            UserAvatar userAvatar = getModel().getRealtimeAgentNode().getUserAvatar(agentKey);

            ImageView agentAvatar;

            agentAvatar = new ImageView();

            Stage toastStage = new Stage();
            toastStage.initStyle(StageStyle.UNDECORATED);
            toastStage.setHeight(120);
            toastStage.setWidth(250);


            BorderPane toastBorderPane = new BorderPane();
            Scene toastScene = new Scene(toastBorderPane);
            toastScene.getStylesheets().add(this.getClass().getResource("MessageToaster.css").toExternalForm());

            if(userAvatar == null || userAvatar.getBuffer() == null)                            
                agentAvatar.getStyleClass().add("AgentDefaultAvatar");
            else            
                agentAvatar.setImage(new Image(new ByteArrayInputStream(userAvatar.getBuffer()), 48, 48, true, true) );

            VBox vboxToastImage = new VBox();
            vboxToastImage.getStyleClass().add("ToasterImage");                
            ImageView imgToastAgent = agentAvatar;
            imgToastAgent.setFitHeight(60);
            imgToastAgent.setFitWidth(60);                
            vboxToastImage.getChildren().add(imgToastAgent);
            toastBorderPane.setLeft(vboxToastImage);

            VBox vboxInCenter = new VBox();
            Label userName = new Label(firstNameLastName);
            userName.getStyleClass().add("ToasterUserName");

            Text toasterContent = new Text(message);
            toasterContent.getStyleClass().add("ToasterMessage");

            vboxInCenter.getChildren().addAll(userName, toasterContent);
            vboxInCenter.getStyleClass().add("ToasterBox");
            toastBorderPane.setCenter(vboxInCenter);
            toastBorderPane.getStyleClass().add("ToasterBorderPane");


            Rectangle2D primaryScreenBounds = Screen.getPrimary().getVisualBounds();

            toastStage.setX(primaryScreenBounds.getMinX() + primaryScreenBounds.getWidth() - TOASTER_WINDOW_WIDTH);
            toastStage.setY(primaryScreenBounds.getMinY() + primaryScreenBounds.getHeight() - TOASTER_WINDOW_HEIGHT);                
            toastStage.setScene(toastScene);
            // check if a chat window for this user already exists
            if (chatAgentsMap.containsKey(agentKey))
            {
                // chat window has been created
                chatController = chatAgentsMap.get(agentKey);
                LOGGER.message(String.format("Showing chat window for agent '%d'.", agentKey));                    
                if(!chatController.getChatStage().isFocused()){
                    toastStage.show();
                    chatController.getChatStage().show();
                }
            }
            else
            {
                // create new chat window                    
                LOGGER.message(String.format("Creating chat window for agent '%d'.", agentKey));                    
                chatController = ChatAgentsController.create(this, agentKey, connectedToOpenfireServer, true);
                toastStage.show();
                toastStage.setAlwaysOnTop(true);

                 //-----Here is where I want that the toastStage or the toastScene is clickable by mouse and that it opens the window already created chatController


                chatController.getChatStage().show();
                chatAgentsMap.put(agentKey, chatController);
            }

            if (chatController != null && message != null)  // append received message to the chat text area
                chatController.addChatEntry(agentKey, message);
        }
        catch (Throwable t)
        {
            LOGGER.error(t);
        }
        finally
        {
            LOGGER.leaveMethod();
        }
    }

我一直尝试使用 CLICK 中的鼠标事件,但我不起作用,因为实例化另一个窗口的变量(chatController.getChatStage().show())不是最终的。

最佳答案

只需删除 chatController 当前的声明和初始化即可:

// ChatAgentsController chatController = null;

并在第一个 if 语句之前声明并初始化一次。也就是说,它实际上是最终的,可以在 lambda 表达式中使用:

boolean controllerExisted = chatAgentsMap.containsKey(agentKey) ;

ChatAgentsController chatController = chatAgentsMap.computeIfAbsent(agentKey, k -> {
    LOGGER.message(String.format("Creating chat window for agent '%d'.", k)); 
    return ChatAgentsController.create(this, k, connectedToOpenfireServer, true);
});

if (controllerExisted) {
    // chat window has been created
    // chatController = chatAgentsMap.get(agentKey);
    LOGGER.message(String.format("Showing chat window for agent '%d'.", agentKey));                    
    if(!chatController.getChatStage().isFocused()){
        toastStage.show();
        chatController.getChatStage().show();
    }
} else {

    // chatController = ChatAgentsController.create(this, agentKey, connectedToOpenfireServer, true);
    toastStage.show();
    toastStage.setAlwaysOnTop(true);

     //-----Here is where I want that the toastStage or the toastScene is clickable by mouse and that it opens the window already created chatController

    toastScene.setOnMouseClicked( e -> chatController.getChatStage().show());

    // chatAgentsMap.put(agentKey, chatController);
}

关于java - 使用场景/舞台作为按钮打开另一个场景/舞台,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41553435/

相关文章:

javafx - [A]如何在javafx中重复播放MP3?

java - Java 10 中的 "var"到底是什么类型的 token ?

java - 我写了 Accessors 和 Mutators 方法,但我仍然无法访问私有(private)变量!为什么?

java反射嵌套对象设置私有(private)字段

java - SmartGWT RestDateSource 和分页(大数据集)动态数据

java - Java 中的图算法

java - 在 javaFX 中的 Togglegroup 上没有按下 toggleButton 时收听

JavaFX ScrollPane 在鼠标输入时接收 ScrollEvents

JavaFX改变TableView选择行栏的蓝色

javafx - 为什么 TableCloumn<S,T> 需要 2 个元素?