Reputation: 1
I'm working for hours now on a ListView that updates on changes by a ChoiceBox. So, I want to pick a group of user (user group has a name shown in the choicebox) and the choicebox-listener gets all usernames from the DB, writes it into a new list and gives it to the ListView which I want now to change. It works fine for the first time, but I cannot change it again.
First I had my ListView integrated by the FXML file created by the SceneBuilder2.0. That didn't work. So I checked the internet and thought I can create just a new ListView-object by = new ListView<>(), but that won't work when the ListView is created by Scenebuilder. So I created a pane via Scenebuilder, added the ListView manually and added it to the pane via .getChildren().add(listView). That didn't work either or let's say I still got the same result.
I tried to edit the ArrayList that is behind the ListView, but I read I'm not supposed to do that. So I changed the ObservableList, but also no success. So I'm currently running out of ideas. In my code below the choicebox (box) got its own list which is working fine (because I do not need to change it). By changing the box-item it will check the DB for the playergroup that is now supposed to show up inside the list. Retrieving the list from the DB is working fine, but it won't change the ListView. If I just reset the ListView by setItems() I'm getting weird output. If I do .clear() and then .setItems() I'm having a NullpointerException.
So I think there might be an issue with the repainting? Could that be? Thanks for any help. This is now my latest piece of code, but I also tried several other ways to make it work.
box.getSelectionModel().selectedIndexProperty().addListener(new ChangeListener<Number>() {
@Override
public void changed(ObservableValue<? extends Number> observable, Number oldValue, Number newValue) {
String selection = box.getItems().get(newValue.intValue());
GetDataDB dbGet = new GetDataDB();
playerList = FXCollections.observableArrayList(dbGet.getPlayerListForTourGrpFromDB(selection));
setPlayerList(playerList);
}
});
public void initListPanes() {
// called at the initialization of the object
playerListView = new ListView<>();
playerPane.getChildren().add(playerListView);
}
public void setPlayerList(ObservableList<Player> playerList) {
playerListView = new ListView<>();
playerListView.setItems(playerList);
playerListView.setCellFactory(playerView -> new PlayerListViewCell());
}
Upvotes: 0
Views: 263
Reputation: 1
Thanks to Slaw, by creating minimal reproducible example, I managed to get it to work. So there is probably something wrong in the logic inside my project.
Below you can find the code of the project that makes excatly what I want: The ChoiceBox changes the list of the ListView.
@FXML
private ListView<String> listView;
@FXML
private ChoiceBox<String> box;
ObservableList<String> observableListViewList;
ObservableList<String> observableChoiceBoxList;
ArrayList<String> maleArrayList;
ArrayList<String> femaleArrayList;
ArrayList<String> choiceBoxArrayList;
public void initLists() {
// choice box init
choiceBoxArrayList = new ArrayList<>();
choiceBoxArrayList.add("Male");
choiceBoxArrayList.add("Female");
observableChoiceBoxList = FXCollections.observableArrayList(choiceBoxArrayList);
box.setItems(observableChoiceBoxList);
// arrayList for ListView
maleArrayList = new ArrayList<>();
maleArrayList.add("Karsten");
maleArrayList.add("Jochen");
maleArrayList.add("Bryan");
femaleArrayList = new ArrayList<>();
femaleArrayList.add("Ingrid");
femaleArrayList.add("Mathilda");
femaleArrayList.add("Christi");
box.getSelectionModel().selectedIndexProperty().addListener(new ChangeListener<Number>() {
@Override
public void changed(ObservableValue<? extends Number> observable, Number oldValue, Number newValue) {
String selection = observableChoiceBoxList.get(newValue.intValue());
if(selection.equals("Male")) {
observableListViewList = FXCollections.observableArrayList(maleArrayList);
listView.setItems(observableListViewList);
} else if(selection.equals("Female")) {
observableListViewList = FXCollections.observableArrayList(femaleArrayList);
listView.setItems(observableListViewList);
}
}
});
}
Upvotes: 0