I have two controllers, one controller has a tableview that lists all members
and the other controller allows a member
to be added. I am having trouble getting the table to update when a member
is added. I use two models that are already given to me, so I don't change anything in the models.
Each member
has an id
and a profession
, I use cellValueFactory
for listing the members
. I tried adding listeners
to the cellValueFactoryProperties
like this:
idColumn.cellValueFactoryProperty().addListener((obs, oldV, newV) -> memberTv.refresh());
But it still doesn't show new members
The Controller with the TableView:
@FXML
TableColumn<Member, Integer> idColumn;
@FXML
TableColumn<Member, String> profColumn;
@FXML
TableView<Member> memberTv;
@FXML
private void initialize() {
memberTv.setItems(getClub().getMembers());
idColumn.setCellValueFactory(cellData -> new
ReadOnlyObjectWrapper(cellData.getValue().getId()));
profColumn.setCellValueFactory(cellData -> new
ReadOnlyStringWrapper(cellData.getValue().getProf()));
}
public final Club getClub() {
\\\returns Club model
}
The Controller that adds members
private int getId() {
return Integer.parseInt(idTf.getText());
}
private void setId(String id) {
idTf.setText(id);
}
private String getProf() {
return profTf.getText();
}
private void setProf(String prof) {
profTf.setText("" + prof);
}
@FXML
public void handleAddMember(){
getClub().addMember(getId(), getProf());
}
When I add a member I want the TableView to show the added member, but it only shows members already added in the model.
I figured it out, I had a ClubController that sets the stage based after a menu button is clicked. The problem was that whenever I set a stage I used a new instance of teh Club model instead of using getKiosk(). I changed all the new Club()```` to getClub()``` and it everything works perfectly now.