簡體   English   中英

我可以在另一個場景中設置一個場景中TextField的值,同時顯示兩個場景嗎?

[英]Can I set the value of a TextField in one scene from another scene, while both scenes are showing?

我正在嘗試從另一個場景中選擇的值的場景填充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();

    }

謝謝。

您可以通過以下方式執行此操作,而無需重新加載基本布局:

  • 從第二個控制器創建對象
  • 在第二個控制器中創建一個方法,以將傳遞的文本字段初始化為本地文本字段。
  • 使用創建的對象和方法將文本字段傳遞給第二個控制器。
  • 在第二個控制器上發生操作時更新文本字段。

這個答案是對這里的代碼和步驟的總結。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM