![](/img/trans.png)
[英]How do I update a TableView in window 1 by an action of a button in window 2 with JavaFX
[英]How to refresh a TableView in window A from a Window B in javaFX? (Edit)
我有一个窗口 A(主窗口),它包含一个 TableView 和一个按钮,单击该按钮时会打开一个新窗口(铭文窗口),其中包含两个文本字段(以设置名字和姓氏)和一个“保存“ 按钮。 我想要发生的是,当我单击题词窗口中的“保存”按钮时,主窗口中的TableView 会刷新并在新行中显示名字和姓氏。
这是我的主窗口控制器:
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();
}
}
这是我的铭文窗口:
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 类:
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;
}
}
我会很感激你的帮助 谢谢
将表的项目列表传递给第二个控制器:
@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()));
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.