如何从另一个FXML控制器获取一个Java FXML控制器中的数据(这些控制器不是嵌套控制器)?

时间:2013-11-09 21:55:05

标签: java javafx-2 javafx viewcontroller fxml

我在一个FXML中有一个按钮,在另一个FXML中有两个文本字段。这两个FXML是独立的,我的意思是它们不是嵌套的。我想在按钮中单击时在控制台/输出中打印文本(位于两个文本字段中)。以下是fxmls及其控制器:

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) {

        }    
    }

如何实现此功能?我考虑到这两个FXML是在两个不同的窗口同时加载的。

2 个答案:

答案 0 :(得分:2)

通过上面的答案(通过SnakeDoc)进行一些修改,我能够实现这一功能。

我创建了一个单独的Singleton类:

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

    }

文本字段在initialize方法中设置。

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

getter方法在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) {

        }    
    }

答案 1 :(得分:1)

将您的控制器变为Singleton,然后使用setter / getter和另一个Controller,然后访问这些值。

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) {

    }    
}

然后在您的其他控制器中,您可以执行以下操作:

TextController.getInstance().getTextField1();

等。