From e74cf2f337ee306d40465c1f44a8717d3877ecfb Mon Sep 17 00:00:00 2001 From: Alex Fedorov Date: Thu, 4 Jul 2019 15:39:55 -0700 Subject: [PATCH] add ability to setup comparator to SortedFilteredList, added example --- src/main/java/tornadofx/Lib.kt | 2 ++ .../testapps/SortableListViewExample.kt | 33 +++++++++++++++++++ 2 files changed, 35 insertions(+) create mode 100644 src/test/kotlin/tornadofx/testapps/SortableListViewExample.kt diff --git a/src/main/java/tornadofx/Lib.kt b/src/main/java/tornadofx/Lib.kt index b733b0be9..4ab06ff1c 100644 --- a/src/main/java/tornadofx/Lib.kt +++ b/src/main/java/tornadofx/Lib.kt @@ -38,9 +38,11 @@ class SortedFilteredList( val items: ObservableList = FXCollections.observableArrayList(), initialPredicate: (T) -> Boolean = { true }, val filteredItems: FilteredList = FilteredList(items, initialPredicate), + val comparatorProperty: ObservableValue>? = null, val sortedItems: SortedList = SortedList(filteredItems)) : ObservableList { init { + comparatorProperty?.let { sortedItems.comparatorProperty().bind(it)} items.onChange { refilter() } } diff --git a/src/test/kotlin/tornadofx/testapps/SortableListViewExample.kt b/src/test/kotlin/tornadofx/testapps/SortableListViewExample.kt new file mode 100644 index 000000000..4eb3a7c93 --- /dev/null +++ b/src/test/kotlin/tornadofx/testapps/SortableListViewExample.kt @@ -0,0 +1,33 @@ +package tornadofx.testapps + +import javafx.beans.property.SimpleObjectProperty +import tornadofx.* +import java.util.* +import kotlin.math.roundToInt + + +class SortableListViewExample : View("Sortable List View") { + private val comparatorProperty = SimpleObjectProperty>(compareBy { it }) + private val numbers = SortedFilteredList(comparatorProperty = comparatorProperty) + + override val root = vbox { + + button("Add number").action { + numbers.add((Math.random() * 100).roundToInt()) + } + button("Reverse Order").action { + comparatorProperty.set(comparatorProperty.get().reversed()) + } + button("Show numbers that are greater than 50").action { + numbers.predicate = { it > 50 } + } + button("Show all numbers").action { + numbers.predicate = { true } + } + + scrollpane { + listview(numbers) { + } + } + } +}