Skip to content
This repository has been archived by the owner on Sep 28, 2024. It is now read-only.

add ability to setup comparator to SortedFilteredList, added example #1024

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions src/main/java/tornadofx/Lib.kt
Original file line number Diff line number Diff line change
Expand Up @@ -38,9 +38,11 @@ class SortedFilteredList<T>(
val items: ObservableList<T> = FXCollections.observableArrayList(),
initialPredicate: (T) -> Boolean = { true },
val filteredItems: FilteredList<T> = FilteredList(items, initialPredicate),
val comparatorProperty: ObservableValue<java.util.Comparator<T>>? = null,
val sortedItems: SortedList<T> = SortedList(filteredItems)) : ObservableList<T> {

init {
comparatorProperty?.let { sortedItems.comparatorProperty().bind(it)}
items.onChange { refilter() }
}

Expand Down
33 changes: 33 additions & 0 deletions src/test/kotlin/tornadofx/testapps/SortableListViewExample.kt
Original file line number Diff line number Diff line change
@@ -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<Comparator<Int>>(compareBy { it })
private val numbers = SortedFilteredList<Int>(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) {
}
}
}
}