I'm always trying to get better at tornadofx. it's...
# tornadofx
h
I'm always trying to get better at tornadofx. it's a hard library to learn if you aren't already comfortable with observable lists and javafx. anyhow, can someone tell me if there's a simple way to have an observable list automatically mapped into drawer items in a drawer?
m
Looking at the code, the drawer inherits from borderpane and contains a list of drawer items
Copy code
val items = FXCollections.observableArrayList<DrawerItem>()
You should be able to access that list and change it yourself
♥️ 1
h
hmm, alright. i think i need more experience with observables. how can i take an observable list of A and map it automatically to an observable list of B via a mapping function A -> B?
m
You can do
Copy code
Bindings.bindContent(A, B)
between two lists of the same type to keep the lists in sync
h
hmm, but not from type A to B?
that would be an exceptionally useful method, i'd think
m
I'm sure there is a way, but I don't know it off hand, let me take a look
h
thank you. i always feel guilty when people are looking up something for me, because i could have done that on my own, but i'm not as certain what to look for as you are
m
no worries, it's usually something I need to know at some point anyway
😄 1
looks like tornadofx provides something do to this
Copy code
/**
 * Bind this list to the given observable list by converting them into the correct type via the given converter.
 * Changes to the observable list are synced.
 */
fun <SourceType, TargetType> MutableList<TargetType>.bind(sourceSet: ObservableSet<SourceType>, converter: (SourceType) -> TargetType): SetConversionListener<SourceType, TargetType> {
trying to figure out the syntax
👍 1
actually, that is for a set not a list, the list looks like
Copy code
fun <SourceTypeKey, SourceTypeValue, TargetType> MutableList<TargetType>.bind(
        sourceMap: ObservableMap<SourceTypeKey, SourceTypeValue>,
        converter: (SourceTypeKey, SourceTypeValue) -> TargetType
): MapConversionListener<SourceTypeKey, SourceTypeValue, TargetType> {
h
thank you so much
m
nope, that's for a map here is the list for real
Copy code
fun <SourceType, TargetType> MutableList<TargetType>.bind(sourceList: ObservableList<SourceType>, converter: (SourceType) -> TargetType): ListConversionListener<SourceType, TargetType> {
ok, this syntax at least is valid
haven't tested though
Copy code
val targetList = mutableListOf<String>().asObservable()
        val sourceList = mutableListOf<Int>().asObservable()
        targetList.bind(sourceList) {
            it.toString()
        }
any int added to source list should get converted to string and added to targetList
I have to step out, let me know if that works
👍 1
h
fingers crossed
works for adding. haven't tested removing
👍 1