Search code examples
listviewbindingjavafxselectedindex

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 index of each is always the same.


Solution

  • 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;
        }
    }