简体   繁体   中英

Using a Scene/Stage as Button to open another Scene/Stage

I'm having difficulty in resolving this problem, and I can't find a proper answer to this problem I'm having. I want that a scene/stage is used as button to open another scene/stage, and both are called on this same method. Here's the full method:

 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();
        }
    }

I kept trying to use the mouse event from CLICK but I doesn't work, because my variable that instantiate the other window(chatController.getChatStage().show()) is not final.

Just remove the current declaration and initialization of chatController :

// ChatAgentsController chatController = null;

and declare and initialize it once, right before the first if statement. That was it is effectively final and can be used in a lambda expression:

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);
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM