views:

152

answers:

2

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.

Thanks!

Andrew Davis NASA - Kennedy Space Center Kettering University Co-op

A: 

"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.

Honza
How can I set the selectedIndex though? It appears they don't have write access according to the API. So I'm not sure how to implement it. Sorry! Thanks!
Xystus7777
Honza, this is not working. selectedIndex is RO property only. And your next suggestion makes infinite loop. (better said, causes StackOverflowError)
Rastislav Komara
A: 

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;
    }
}
Rastislav Komara
Interesting, I'll give that a try, thanks for your answer! Another thing however, I've noticed that in JavaFX 1.2 it doesn't appear to be possible to manipulate the scrollbar, so I could potentially select the 100th index, but I wouldn't be able to see it. (To the best of my knowledge.) Thanks!
Xystus7777
This definitely worked, thanks a lot! Only issue is the scrollbar!
Xystus7777