tags:

views:

108

answers:

2

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?

A: 

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?

tulskiy
+1  A: 

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.

Adamski
My bad. I actually edited the previous question, assuming you will be notified about it. I did use the code exactly as you had given. It didn't work. Will give it a shot again and let you know.
Goutham