[英]Can I set the value of a TextField in one scene from another scene, while both scenes are showing?
I'm trying to populate the TextField from a scene with the value selected in another scene. 我正在尝试从另一个场景中选择的值的场景填充TextField。
So there's a scene (I'll call it parent scene) with a TextField and a button which opens another scene (I'll call it child scene). 所以有一个场景(我称之为父场景)有一个TextField和一个打开另一个场景的按钮(我称之为子场景)。 The child scene has a TableView which selected value I want to set to the parent scene's TextField. 子场景有一个TableView,它选择了我想要设置为父场景的TextField的值。 I know how to do this only if the scene with the TextField is not already opened, in my case the child scene's stage being setted as showAndWait. 我知道只有在没有打开TextField的场景时才会这样做,在我的例子中,子场景的舞台被设置为showAndWait。 So I think I need to get the parent scene's stage and set it to the stage field in select() method below from the child controller. 所以我认为我需要获取父场景的舞台并将其设置为子控制器下面的select()方法中的舞台字段。
// method from child controller, called when selecting a row from the tableview //来自子控制器的方法,从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();
} });
// this is the method for the button from parent scene that opens the child scene //这是打开子场景的父场景按钮的方法
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();
}
Thanks. 谢谢。
You can do this without any need to reload the base layout, by: 您可以通过以下方式执行此操作,而无需重新加载基本布局:
This answer is a summarization of the codes and steps here . 这个答案是对这里的代码和步骤的总结。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.