开发者

How to make JTree selection model only allow sibling selection?

I have a jtree and I only want the user to be able to select sibling nodes. If the user selects nodes that aren't siblings, I want the previously selected nodes to be deselect开发者_如何学JAVAed. The user should also be able to select sibling nodes that aren't contiguous. I was hoping to extend defaulttreeselectionmodel.

Is there a simple way to do this?


Seems like you'll have to play with TreeSelectionModel (which will store current selection state) and TreeSelectionListener. An example algorithm could be the following : when your selection change, if your TreeSelectionListener is not in active mode, it enters into that mode (this is useful as your listener will update selection in some cases, and you don't want crazy cycles, want you ?). Then, it checks selected nodes using TreeSelectionModel#getSelectionPaths(). If this array size is one, only one node is selected and all is OK. If its size si greater than 1, then you'll have to write some code to ensure those nodes are siblings (as an example by ensuring their TreePath are identical except the last part.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜