开发者

Java Problem with the clearselection() method

开发者 https://www.devze.com 2022-12-12 01:57 出处:网络
I have 2 trees in my program. Iam using JTree\'s clearselection() method to clear the selection in 1 tree when something in the other tree is selected. The main code is something like this:(inside a v

I have 2 trees in my program. Iam using JTree's clearselection() method to clear the selection in 1 tree when something in the other tree is selected. The main code is something like this:(inside a valueChanged event listener and tree being the one on which the current selection has been triggered)

    if ( tree == tree1 ){

        tree2.clearSelection();

    } else {

        tree1.clearSelection();

    }

When I select for the first time, it works fine. But when I try to select from a differen开发者_运维百科t tree after this, it appears the valueChanged method is getting called twice. Any solution?


Did you use the code I gave you in this question?

This included a flag to get round the problem of introducing an infinite loop, and should also ignore subsequent selection events when it is processing the current one.

BTW Given this relates directly to the previous question you may have been better off just commenting on the previous post. That way all the context is kept in one place.


clearSelection() triggers valueChanged as well, so you need a workaround, add some flag and do not clear selection when it's true.

EDIT. Seems like using some flag is tricky. Can you use MouseListener instead and run same code within mouseClicked event?

0

精彩评论

暂无评论...
验证码 换一张
取 消