I have two windows and two controllers.
In the first window is a button which opens a second window and TableView. The second window is a form where I insert properties of my class. After inserting I click on "Save" and add new created object to a static list. This should be shown in a TableView. When I use only one window and one controller everything works fine but splitting the functionality as described in two windows the TableView is not refreshed.
Here is my minimal example:
MAIN CONTROLLER
public class Controller {
@FXML
public TableView tableView;
@FXML
private Button openWindow;
// called by the FXML loader after the labels declared above are injected:
public void initialize() {
TableColumn mailColumn = new TableColumn("E-Mail");
tableView.getColumns().addAll(mailColumn);
mailColumn.setCellValueFactory(new PropertyValueFactory<Person,String>("mail"));
openWindow.setOnAction((event) -> {
new PersonInputForm(event);
});
}
// ###### Receiver TableView Action Handling #######
public void updateReceiverList(){
final ObservableList<Person> data = FXCollections.observableArrayList(Memory.receiverList);
tableView.getItems().clear();
tableView.getItems().addAll(data);
}
}
SECONDARY CONTROLLER
public class PersonInputFormController {
@FXML
private TextField mail;
@FXML
private AnchorPane personInputFormAnchorPane;
private Controller mainController ;
Stage stage;
public void setStage() {
stage = (Stage) personInputFormAnchorPane.getScene().getWindow();
}
public void setMainController(Controller mainController){
this.mainController = mainController;
}
public void save(){
Memory.saveReceiver(mail);
mainController.updateReceiverList();
stage.close();
}
}
MAIN WINDOW
public class Main extends Application {
@Override
public void start(Stage primaryStage) throws Exception{
FXMLLoader loader = new FXMLLoader(getClass().getResource("sample.fxml"));
Parent root = loader.load();
Scene scene = new Scene(root);
primaryStage.setScene(scene);
primaryStage.setResizable(false);
primaryStage.show();
}
public static void main(String[] args) {
launch(args);
}
}
SECONDARY WINDOW
public class PersonInputForm {
public PersonInputForm(ActionEvent event) {
Stage stage = new Stage();
FXMLLoader mainFxmlLoader = new FXMLLoader(getClass().getResource("../sample.fxml"));
try {
mainFxmlLoader.load();
} catch (IOException e) {
e.printStackTrace();
}
Controller mainController = mainFxmlLoader.getController();
FXMLLoader fxmlLoader = new FXMLLoader(getClass().getResource("PersonInputForm.fxml"));
Parent root = null;
try {
root = (Parent)fxmlLoader.load();
} catch (IOException e) {
e.printStackTrace();
}
stage.setScene(new Scene(root));
stage.initOwner(
((Node) (event.getSource())).getScene().getWindow() );
PersonInputFormController controller = fxmlLoader.<PersonInputFormController>getController();
controller.setStage();
controller.setMainController(mainController);
stage.show();
}
}
MEMORY
public class Memory {
public static sample.Person sender = new sample.Person();
public static ArrayList<sample.Person> receiverList = new ArrayList();
static public void saveReceiver(TextField mail){
Person receiver = new Person();
receiver.setMail(mail.getText());
receiverList.add(receiver);
}
}
OBJECT
public class Person {
private SimpleStringProperty mail = new SimpleStringProperty();
public String getMail() {
return mail.get();
}
public SimpleStringProperty mailProperty() {
return mail;
}
public void setMail(String mail) {
this.mail.set(mail);
}
}
I found the following similar sounding topics:
How to set items of a TableView in a different controller class in JavaFX?
Javafx Update a tableview from another FXML
But, I still don't understand how my problem can be solved.
Thanks in advance!