Reputation: 911
Okay i got this javafx app were i got exercises and i can filter them through muscle groups but when its unfiltered my program removes both the object from obseravable list and from tableView but when its filtered it only removes from tableview but the object remains. So next time i filter its back agian.
Here are my observableLists
ObservableList<Exercise> filteredExercises = FXCollections.observableArrayList();
ObservableList<Exercise> exercises = FXCollections.observableArrayList();
Here is the filter method
@FXML
private void filterByChest(ActionEvent event) {
filteredExercises.clear();
for (Exercise xercise : exercises) {
System.out.println(xercise);
if(xercise.getFocusGroup().toLowerCase().contains("chest")){
filteredExercises.add(new Exercise(xercise.getName(), xercise.getFocusGroup(), xercise.getTool(), xercise.getPb()));
}
}
exTable.setItems(filteredExercises);
}
Here is the remove method
@FXML
private void deleteExercise(ActionEvent event) {
Exercise selectedItem = exTable.getSelectionModel().getSelectedItem();
for (Exercise e : exercises){
if(selectedItem == e){
exercises.remove(e);
}
}
exTable.getItems().remove(selectedItem);
System.out.println(exercises);
}
Anyone got a solution to why it wont remove the object
Upvotes: 0
Views: 108
Reputation: 209553
You should use a FilteredList
for this functionality:
// create the lists:
ObservableList<Exercise> exercises = FXCollections.observableArrayList();
// initialize the filtered list with a filter that is always true
// (i.e. no filtering)
ObservableList<Exercise> filteredExercises = exercises.filtered(exercise -> true);
// use the filtered list as the items list for the table:
public void initialize() {
// ...
exTable.setItems(filteredExercises);
// ...
}
// filter by setting the predicate on the filtered list:
@FXML
private void filterByChest(ActionEvent event) {
filteredExercises.setPredicate(
exercise -> exercise.getFocusGroup().toLowerCase().contains("chest"));
}
// manipulate the list by adding/removing elements to/from the underlying list:
@FXML
private void deleteExercise(ActionEvent event) {
Exercise selectedItem = exTable.getSelectionModel().getSelectedItem();
exercises.remove(selectedItem);
}
Upvotes: 2