r/JavaFX • u/Draaksward_89 • Jan 21 '25
Help How to clear an ItemView?
I'm working on a UI part, which is basically a list of directories and its contents

Whenever you select the ChoiceBox, there is a list of subfolders.
I have a ChangeListener hooked up to ChoiceBox, which basically calls
listView.getItems().clear();
listView.getItems().addAll(...);
listView.refresh();If I'm switching content of listView from 10 elements to 3 (from `Interrogator` to `Pariah Nexus`) I'm getting leftovers.
And the leftovers are not clickable. Just a graphical glitch.
How to address this?
1
Upvotes
View all comments
Show parent comment
1
u/xdsswar Jan 21 '25
do not use c/c++ code style in java , use camel-case , example fileListFolder.
Regarding the code, javafx listviews and tableviews support custom cell factories, like this :
private static class CustomListCell extends ListCell<String> {
protected void updateItem(String item, boolean empty) {
super.updateItem(item, empty);
if (empty || item == null) {
setText(null);
setGraphic(null);
} else {
// Customize how each item is displayed
setText(item);
setStyle("-fx-text-fill: blue; -fx-font-weight: bold;"); // Example styling
}
}
}
if the item is null, not cell is created, if not null a custom cell is created with styles, etc, whatever you want. Tis can be used to any ListView , no matter if its from a ComboBox