ListView selection remains persistent after exiting choice mode
The main reason for the problem is that once the ListView
selection mode is switched to CHOICE_MODE_NONE
, the framework optimizes out the clear operation as it is no longer supporting 'selections'. I have improved the above workarounds a bit by clearing the selection state manually and then setting the mode in a delayed manner so the framework will have its turn to clear the state before turning the mode to CHOICE_MODE_NONE
.
final ListView lv = getListView();
lv.clearChoices();
for (int i = 0; i < lv.getCount(); i++)
lv.setItemChecked(i, false);
lv.post(new Runnable() {
@Override
public void run() {
lv.setChoiceMode(ListView.CHOICE_MODE_NONE);
}
});
Preserve checked items listview filter
You need to maintain the state of CheckBox manually due to the recycling behaviour of ListView. I had already written a blog for the same explaining how you can manage that.
ListView with CheckBox Scrolling Issue
Mimicking CTRL+Click Multiple Selection in ListView using Javafx
You could handle changing the selection when a user clicks a ListCell
yourself instead of using the standard event handling:
@Override
public void start(Stage primaryStage) {
ListView<Integer> listView = new ListView<>();
listView.getSelectionModel().setSelectionMode(SelectionMode.MULTIPLE);
listView.getItems().setAll(1, 2, 3, 4, 5, 6, 7, 8, 9, 10);
listView.addEventFilter(MouseEvent.MOUSE_PRESSED, evt -> {
Node node = evt.getPickResult().getIntersectedNode();
// go up from the target node until a list cell is found or it's clear
// it was not a cell that was clicked
while (node != null && node != listView && !(node instanceof ListCell)) {
node = node.getParent();
}
// if is part of a cell or the cell,
// handle event instead of using standard handling
if (node instanceof ListCell) {
// prevent further handling
evt.consume();
ListCell cell = (ListCell) node;
ListView lv = cell.getListView();
// focus the listview
lv.requestFocus();
if (!cell.isEmpty()) {
// handle selection for non-empty cells
int index = cell.getIndex();
if (cell.isSelected()) {
lv.getSelectionModel().clearSelection(index);
} else {
lv.getSelectionModel().select(index);
}
}
}
});
Scene scene = new Scene(listView);
primaryStage.setScene(scene);
primaryStage.show();
}
Related Topics
How to Change the Edittext Text Without Triggering the Text Watcher
How to Close Another App in Android
Kotlin-Android: Unresolved Reference Databinding
How to Override the Cipherlist Sent to the Server by Android When Using Httpsurlconnection
Android - Viewrootimpl$Calledfromwrongthreadexception
How to Get Ruby Generated Hmac for Sha256 That Is Url Safe to Match Java
Detecting Device Type in a Web Application
How to Set -Source 1.7 in Android Studio and Gradle
How to Create My Own Appender in Log4J
Why Menuitemcompat.Getactionprovider Returns Null
How to Create JSON Object Using String
Use Startactivityforresult from Non-Activity
Why Does My Compare Method Throw Exception -- Comparison Method Violates Its General Contract!