如何在 javaFX 中从 Window B 刷新 window A 中的 TableView? (编辑)

How to refresh a TableView in window A from a Window B in javaFX? (Edit)

我有一个 window A (main Window) 它包含一个 TableView 和一个按钮,单击按钮时它会打开一个新的 window (inscription window) 包含两个文本字段(为了设置名字和姓氏)和一个“保存”按钮。我想要发生的是当我单击 题词 window 中的“保存”按钮时,main window[= 中的 TableView 27=] 得到刷新并在新行中同时显示名字和姓氏。

这是我的主要 window 控制器:

public class FxmlMainController implements Initializable {

    @FXML
    private TableView<DataArray> dataTable;
    
    @FXML
    private TableColumn<DataArray, String> firstNameCol;
    @FXML
    private TableColumn<DataArray, String> lastNameCol;
    
    public static ObservableList<DataArray> DATA_LIST;

    @Override
    public void initialize(URL url, ResourceBundle rb) {

       DATA_LIST = FXCollections.observableArrayList(new DataArray("Michael", "Jackson")); 
       firstNameCol.setCellValueFactory(new PropertyValueFactory<DataArray, String>("firstName"));
       lastNameCol.setCellValueFactory(new PropertyValueFactory<DataArray, String>("lastName"));
       dataTable.getItems().addAll(DATA_LIST);
    }    

    //Open the inscription window
    @FXML
    private void openInscriptionWindow(ActionEvent event) throws IOException {
        Parent root = FXMLLoader.load(getClass().getResource("/inscriptionui/FxmlInscription.fxml"));
        Scene scene = new Scene(root);
        Stage stage = new Stage();
        stage.setScene(scene);
        stage.show();
    }
    
}

这是我的题词window:

public class FxmlInscriptionController implements Initializable {

    @FXML
    private TextField firsNametInput;   //first name TextField

    @FXML
    private TextField lastNameInput;   //last name TextField

    @Override
    public void initialize(URL url, ResourceBundle rb) {
        // TODO
    }

    //Show the inserted first name and last name in my TableView in the main window
    @FXML
    private void saveData(MouseEvent event) throws IOException {
   
    FxmlMainController.DATA_LIST = FXCollections.observableArrayList(new DataArray(firsNametInput.getText(), lastNameInput.getText()));

        /*
        what should I write more here to make my TableView
        get refreshed automatically and show the new inserted values?
         */
    }

}

这是我的 DataArray class:

public class DataArray {

    private String firstName;
    private String lastName;

    public DataArray(String firstName, String lastName) {
        this.firstName = firstName;
        this.lastName = lastName;
    }

    public String getFirstName() {
        return firstName;
    }

    public void setFirstName(String firstName) {
        this.firstName = firstName;
    }

    public String getLastName() {
        return lastName;
    }

    public void setLastName(String lastName) {
        this.lastName = lastName;
    }

}

非常感谢你的帮助 谢谢

将table的项目列表传递给第二个控制器:

@FXML
private void openInscriptionWindow(ActionEvent event) throws IOException {
    FXMLLoader loader = new FXMLLoader(getClass().getResource("/inscriptionui/FxmlInscription.fxml"));
    Parent root = loader.load();

    FxmlInscriptionController controller = loader.getController();
    controller.setTableItems(dataTable.getItems());

    Scene scene = new Scene(root);
    Stage stage = new Stage();
    stage.setScene(scene);
    stage.show();
}

然后简单地将新项目添加到列表中:

public class FxmlInscriptionController implements Initializable {

    @FXML
    private TextField firsNametInput;   //first name TextField

    @FXML
    private TextField lastNameInput;   //last name TextField

    private ObservableList<DataArray> tableItems ;

    public void setTableItems(ObservableList<DataArray> tableItems) {
        this.tableItems = tableItems ;
    }

    @Override
    public void initialize(URL url, ResourceBundle rb) {
        // TODO
    }

    //Show the inserted first name and last name in my TableView in the main window
    @FXML
    private void saveData(MouseEvent event) throws IOException {
   
        tableItems.add(new DataArray(firsNametInput.getText(), lastNameInput.getText()));

    }

}