简体   繁体   English

我可以在另一个场景中设置一个场景中TextField的值,同时显示两个场景吗?

[英]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: 您可以通过以下方式执行此操作,而无需重新加载基本布局:

  • Creating an object from the second controller 从第二个控制器创建对象
  • Create a method in the second controller to init the passed textfield to a local one. 在第二个控制器中创建一个方法,以将传递的文本字段初始化为本地文本字段。
  • Pass your textfield to the second controller with the created object and method. 使用创建的对象和方法将文本字段传递给第二个控制器。
  • Update your textfield when action occur on the second controller. 在第二个控制器上发生操作时更新文本字段。

This answer is a summarization of the codes and steps here . 这个答案是对这里的代码和步骤的总结。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Scene Builder 8.0和Intelli。 从一个场景到另一个场景 - Scene Builder 8.0 & Intelli. From one scene to another scene 如何在JavaFx中将数据从一个场景传输到另一个场景? - How do I transfer data from one scene to another in JavaFx? 如何设置场景中心的按钮? - How I can set the buttons on the center of the scene? 无法从Javafx中的另一个控制器更新场景 - Can Not update scene from another controller in javafx 您如何将两个场景组合为一个场景? - How do you combine two scenes into one scene? 将数据从一个场景传递到另一个场景不起作用? (以与其他所有人相同的方式进行) - Passing data from one scene to another isn't working? (doing it the same way i have for all others) 如何设置 MediaView 的大小,以便所有视频都适合场景并能与场景成比例增长? - How do I set the size of the MediaView so that all videos fit in the scene and can grow proportionally to the scene? Java-场景构建器-切换场景后,TextField和ComboBox中的用户输入消失 - Java - Scene Builder - User input in TextField and ComboBox disappears after switching a scenes 如何将JavaFX WebView设置为与场景一样大? - How can I set a JavaFX WebView as big as the scene? JavaFX 无法从另一个线程更改场景 - JavaFX can't change scene from another thread
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM