In JavaFX, how can I bind two listview's so their selectedIndex is always the same?
I have two (or more) ListView's that are side by side. I need them to act as one so the selected in开发者_如何转开发dex of each is always the same.
This should work :), maybe.
var lv1 = ListView {
}
var lv2 = ListView {
}
var onSync = false;
var sel1 = bind lv1.selectedIndex on replace {
if (not onSync) {
onSync = true;
lv2.select(sel1);
onSync = false;
}
}
var sel2 = bind lv2.selectedIndex on replace {
if (not onSync) {
onSync = true;
lv1.select(sel2);
onSync = false;
}
}
"bind with inverse" seems to be an option:
var a;
var b = bind a with inverse;
works only for simple expressions. anything more complex will generate a warning/error.
Except that it isn't because of ListView's selectedIndex is public-read (thaks for the correction).
You will have to do it like this:
var lv1 = ListView {
}
var lv2 = ListView {
}
var sel1 = bind lv1.selectedIndex on replace {
lv2.select(sel1);
}
var sel2 = bind lv2.selectedIndex on replace {
lv1.select(sel1);
}
You also may want to add some ifs here and there to avoid extra select() calls.
精彩评论