I have a controller with a view that has a combobox with customernames, both generated by FXMLLoader. When I click the "create new customer' button, newCustomerPane()
is called which opens a 2nd view. This view allows the user to define and add a new customer. When the user is done (clicks on "save") addCustomer()
is called which adds the new customer to the database. addCusomter() then calls
update` which fills the combobox in the 1st view with an updated list of customers.
The problem is that the 1st view simply doesn't update. I've tried updating the 2nd view as a test and that seems to work fine. I also tested if the controller succeeds in adding the customer to the database and that works fine too. I'm also sure that the controller manages to retrieve the ew updat from the database.
Here is the method that instantiates the controller and the view:
public class Run extends Application{
public static void main(String[] args) {
Application.launch(args);
}
@Override
public void start(Stage primaryStage) throws Exception {
FXMLLoader loader = new FXMLLoader();
Parent root = loader.load(getClass().getResource("ProjectBeheer.fxml"));
Scene scene = new Scene(root);
primaryStage.setScene(scene)
primaryStage.show();
ProjectBeheerController projectBeheerController=loader.getController();
}
}
Here is my controller: public class ProjectBeheerController {
CustomerDAO customerDao = new CustomerDAO();
@FXML Button editBtn;
@FXML ComboBox<String> klantCB;
ObservableList<String> obList = FXCollections.observableArrayList("klant1","klant2");
@FXML Button newCustomer;
@FXML Button newProject;
@FXML Button newSprint;
@FXML Button newUserStory;
Stage customerStage = new Stage();
@FXML TextField custName;
@FXML TextArea custDescription;
@FXML Button custAddBtn;
@FXML Button custCancelBtn;
public ProjectBeheerController() {
klantCB = new ComboBox<String>();
}
@FXML
private void initialize() {
klantCB.setValue("klant");
update();
}
private void update(){
ArrayList<CustomerModel> customers = customerDao.getCustomerList();
ObservableSet<String> observableSet = FXCollections.observableSet();
for(CustomerModel customer: customers) {
observableSet.add(customer.getCustomer_name());
}
klantCB.setItems(FXCollections.observableArrayList(observableSet));
}
public void newCustomerPane(){
try {
Parent root = null;
root = FXMLLoader.load(getClass().getResource("ProjectBeheer_KlantEditor.fxml"));
Scene scene = new Scene(root);
customerStage.setScene(scene);
customerStage.show();
} catch (IOException e) {
e.printStackTrace();
}
}
public void addCustomer(){
custName.setText(custName.getText()+" update!");
customerDao.addCustomer(custName.getText(),custDescription.getText());
this.update();
hideCustomerPane();
}
}
I've searched the internet for a solution, but no one seems to share this problem. I hope anyone can help me. Feel free to ask for code if you want to see more.