我正在尝试使用在另一个场景中选择的值从一个场景填充 TextField。
所以有一个场景(我称之为父场景),带有一个 TextField 和一个打开另一个场景的按钮(我称之为子场景)。子场景有一个 TableView,我想将其选择的值设置为父场景的 TextField。只有当带有 TextField 的场景尚未打开时,我才知道如何执行此操作,在我的例子中,子场景的舞台被设置为 showAndWait。所以我想我需要获取父场景的舞台并将其设置为子控制器下面 select() 方法中的舞台字段。
// 来自子控制器的方法,当从 tableview 中选择一行时调用
public void select() throws IOException
{
Class class = (Class)table.getSelectionModel().getSelectedItem();
FXMLLoader loader = new FXMLLoader();
loader.setLocation(getClass().getClassLoader().getResource("parentStage.fxml"));
Parent parent = loader.load();
Stage stage = // here i need to pass the parent stage I think;
stage.initModality(Modality.APPLICATION_MODAL);
ParentController parentController = loader.getController();
table.setOnMouseClicked(event -> {
if(event.getClickCount()==2){
parentController.setTextField(new TextField(class.getValue()));
stage.setScene(new Scene(parent));// also,
I think, here it should be passed the already opened parent scene
stage.show();
((Node)(event.getSource())).getScene().getWindow().hide();
} });
// 这是打开子场景的父场景按钮的方法
public void add() throws IOException
{
Parent parent = FXMLLoader.load(getClass().getClassLoader().getResource("childStage.fxml"));
stage = new Stage();
stage.initModality(Modality.APPLICATION_MODAL);
stageProdusNou.setScene(new Scene(parent));
stageProdusNou.showAndWait();
}
MM们
相关分类