Goutham
Goutham

Reputation: 2859

Java Problem with the clearselection() method

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 different tree after this, it appears the valueChanged method is getting called twice. Any solution?

Upvotes: 0

Views: 1827

Answers (2)

Adamski
Adamski

Reputation: 54697

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.

Upvotes: 1

Denis Tulskiy
Denis Tulskiy

Reputation: 19167

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?

Upvotes: 0

Related Questions