javafx变量在两个控制器之间传输(在不同的控制器中设置和获取数据) - 更新

时间:2014-06-13 07:03:09

标签: java javafx javafx-2 javafx-8

我正在尝试将变量从一个控制器传输到另一个控制器,但我没有得到正确的输出:

我的代码:

   public class CustomControl extends AnchorPane implements Initializable {
    String customId;

    public CustomControl() {
        //if you want to set a FXML
        FXMLLoader fxmlLoader = new FXMLLoader(getClass().getResource("/res/customControl.fxml"));
        //Defines this class as the controller
        fxmlLoader.setRoot(this);
        //this.getStylesheets().add("/res/style.css"); <- if you want to set a css
        fxmlLoader.setController(this);
        try {
            fxmlLoader.load();
        } catch (IOException exception) {
            throw new RuntimeException(exception);
        }

    }
        public String getCustomId() {
            return customId;
        }
    public void setCustomId(String customId) {
        return this.customId = customId;
    }
    @Override
    public void initialize(URL arg0, ResourceBundle arg1) {
          //Initializes the controller
    }
}

在其他控制器上设置CustomId变量

CustomControl c = new CustomControl();
c.setCustomId("StackOverflow");

从其他控制器获取CustomId变量

CustomControl c = new CustomControl();
c.getCustomId();
System.out.Println(c.getCustomId());

它给我输出

null

但要求是

StackOverflow

0 个答案:

没有答案