我正在尝试将变量从一个控制器传输到另一个控制器,但我没有得到正确的输出:
我的代码:
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