JavaFX Change label text from another class with controller

后端 未结 1 471
终归单人心
终归单人心 2020-12-21 20:28

I want to change the text of a Label with the controller from another class. I have made a method in the FXMLDocumentController, which sets the text to the label:

         


        
相关标签:
1条回答
  • 2020-12-21 21:04

    You are not updating the label because you are creating another instance of FXMLDocumentController when you use the FXMLoader.

    You should set the controller instance, that contains the label, as a parameter to the other Class.

    Below you have the code that could solve your need. Here I set the Controller instance to the Connector class, so you can call the setLabelText method from the other class:

    public class Connector {
        public static void Connecting(FXMLDocumentController controller) {
            try {
                System.out.println("Connector.Connecting(): Called");
                controller.setLabelText("Bye World");
            } catch (IOException ex) {
                Logger.getLogger(Connector.class.getName()).log(Level.SEVERE, null, ex);
            }
        }
    }
    
    
    public class FXMLDocumentController implements Initializable {
    
        @FXML
        private Label label;
    
        @FXML
        private void handleButtonAction(ActionEvent event) {
            System.out.println("FXMLDocumentController.#handleButtonAction");
            label.setText("Hello World!");
            Connector.Connecting(this);
        }
    
        @Override
        public void initialize(URL url, ResourceBundle rb) {
            // TODO
        }    
    
        public void setLabelText(String text)
        {
            System.out.println("FXMLDocumentController.setLabelText(): Called");
            label.setText(text);
        }
    
    }
    

    Note:

    If your routine is going to take longer to execute whatever it needs to, you might want to use a Task, so you don't freeze your UI. To update the Label, you have to bind the text property and then update the Text value using the updateMessage() method.

    public class FXMLDocumentController implements Initializable {
    
        @FXML
        private Label label;
    
        @FXML
        private void handleButtonAction(ActionEvent event) {
            System.out.println("FXMLDocumentController.#handleButtonAction");
            label.setText("Hello World!");
    
            Task<Boolean> connectorTask = new ConnectorTask();
            label.textProperty().bind(connectorTask.messageProperty());
            connectorTask.setOnSucceeded(e -> {
                // this is going to be called if the task ends up without error
                label.textProperty().unbind();
            });
            new Thread(connectorTask).start();
        }
    
        @Override
        public void initialize(URL url, ResourceBundle rb) {
            // TODO
        }    
    
        //public void setLabelText(String text)
        //{
        //    System.out.println("FXMLDocumentController.setLabelText(): Called");
        //    label.setText(text);
        //}
    
    
        public class ConnectorTask extends Task<Boolean> {
    
            @Override
            protected Boolean call() throws Exception {
                // ... do whatever you need here
    
                // then you call this method to update the TextProperty from the Label that was bound.
                updateMessage("Bye World");
    
                return Boolean.TRUE;
            }
        }
    
    }
    

    NOTE:

    There is a possible duplicate question for this, please see my answer for this question here!

    0 讨论(0)
提交回复
热议问题