Issue
I have a window A (main Window) it contains a TableView and a button, when the button is clicked it opens a new window (inscription window) that contains two TextFields (in order to set first name and last name) and a "save" button. What I want to happen is when I click the "save" button in the inscription window, my TableView in the main window gets refreshed and shows both first name and last name in a new row.
Here is my main window controller:
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();
}
}
Here is my inscription 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?
*/
}
}
And here is my 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;
}
}
I would appreciate your help Thank you
Solution
Pass the table's item list to the second controller:
@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();
}
and then simply add the new item to the list:
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()));
}
}
Answered By - James_D
Answer Checked By - Terry (JavaFixing Volunteer)