How to get data in one Java FXML Controller from another FXML Controller (these controllers are not nested controllers)? [duplicate]

妖精的绣舞 提交于 2020-01-06 14:00:16

问题


I have one button in one FXML and two text fields in another FXML. These two FXMLs are independent, I mean they are not nested. I want to print the text (which are in the two text fields) in the console/output whenever there is a click in the button. Below are the fxmls and their controllers:

Button.fxml

    <AnchorPane id="AnchorPane" prefHeight="200.0" prefWidth="320.0" xmlns:fx="http://javafx.com/fxml" fx:controller="textboxandbuttonbinding.ButtonController">
      <children>
         <Button fx:id="button" layoutX="126.0" layoutY="90.0" onAction="#handleButtonAction" text="Button" />
      </children>
    </AnchorPane>

ButtonController.java

    public class ButtonController implements Initializable {

        @FXML
        private void handleButtonAction(ActionEvent event) {

        }

        @Override
        public void initialize(URL url, ResourceBundle rb) {

        }    
    }

Text.fxml

    <AnchorPane id="AnchorPane" prefHeight="400.0" prefWidth="600.0" xmlns:fx="http://javafx.com/fxml" fx:controller="textboxandbuttonbinding.Sample1111Controller">
       <children>
           <TextField fx:id="textField1" layoutX="186.0" layoutY="133.0" prefWidth="200.0" promptText="text 1" />
           <TextField fx:id="textField2" layoutX="186.0" layoutY="200.0" prefWidth="200.0" promptText="text2" />
       </children>
    </AnchorPane>

TextController.java

    public class TextController implements Initializable {
        @FXML
        private TextField textField1;
        @FXML
        private TextField textField2;

        @Override
        public void initialize(URL url, ResourceBundle rb) {

        }    
    }

How can I achieve this functionality? I have taken into consideration that these two FXMLs are loaded at the same time as two different windows.


回答1:


With some modification in above answer (by SnakeDoc), i am able to achieved the functionality.

I have created a separate Singleton class:

Singleton.java

    public class Singleton {
        private static Singleton instance = new Singleton();
        public static Singleton getInstance(){
            return instance;
        }

        private TextField txtField1;
        private TextField txtField2;

        public TextField getTxtField2() {
            return txtField2;
        }

        public void setTxtField2(TextField txtField2) {
           this.txtField2 = txtField2;
        }

        public TextField getTxtField1() {
           return txtField1;
        }

       public void setTxtField1(TextField txtField1) {
           this.txtField1 = txtField1;
       }

    }

The text fields are set in the initialize method.

TextController.java

    public class TextController implements Initializable {

       @FXML
       private TextField textField1;

       @FXML
       private TextField textField2;

       @Override
       public void initialize(URL url, ResourceBundle rb) {
            Singleton.getInstance().setTxtField1(textField1);
            Singleton.getInstance().setTxtField2(textField2);
       }    
    }

The getter methods are called in the class ButtonController.java

ButtonController.java

    public class ButtonController implements Initializable {    

        @FXML
        private void handleButtonAction(ActionEvent event) {
            String str1 = Singleton.getInstance().getTxtField1().getText();
            String str2 = Singleton.getInstance().getTxtField2().getText();

            System.out.println(str1);
            System.out.println(str2);

        }

        @Override
        public void initialize(URL url, ResourceBundle rb) {

        }    
    }



回答2:


Turn your controller into a Singleton, then have a setter/getter and the other Controller and then access the values.

public class TextController implements Initializable {

    private TextController instance;

    private TextController() {};

    public static TextController getInstance() {
        if (TextController.instance == null) {
            synchronized (TextController.class) {
                if (TextController.instance == null) {
                    TextController.instance = new TextController();
                }
            }
        }
        return TextController.instance;
    }

    @FXML
    private TextField textField1;

    public TextField getTextField1() {
        return textField1;
    }
    @FXML
    private TextField textField2;

    public TextField getTextField2() {
        return textField2;
    }

    @Override
    public void initialize(URL url, ResourceBundle rb) {

    }    
}

Then in your other controller you can do something like:

TextController.getInstance().getTextField1();

etc.



来源:https://stackoverflow.com/questions/19883563/how-to-get-data-in-one-java-fxml-controller-from-another-fxml-controller-these

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!