aspose file tools*
The moose likes Swing / AWT / SWT and the fly likes Custom Multiple Selection for JTree Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "Custom Multiple Selection for JTree" Watch "Custom Multiple Selection for JTree" New topic
Author

Custom Multiple Selection for JTree

Vitaly Karnyushin
Greenhorn

Joined: Apr 23, 2010
Posts: 1
Hi,

I need to implement specific multiple selection behavior.
The multiple selection should be worked for the nodes of one level.
As an illustration, if I have two expanded nodes with some amount of children in my view and one of the parents is already selected then multiple selection action should select only second one parent, children should be ignored.

Any advices how to properly implement that?

Thanks,
Vitaly
Brian Cole
Author
Ranch Hand

Joined: Sep 20, 2005
Posts: 862
John Golt wrote:I need to implement specific multiple selection behavior.
The multiple selection should be worked for the nodes of one level.
As an illustration, if I have two expanded nodes with some amount of children in my view and one of the parents is already selected then multiple selection action should select only second one parent, children should be ignored.

Any advices how to properly implement that?


I'm not I understand exactly what you want (Do you really want selection to behave differently depending on whether a node is currently visible in the view??) but have you looked at the TreeSelectionModel interface? You can write your own and do anything you want in the addSelectionPath() addSelectionPaths() methods, including ignoring certain paths, or substituting others, or deselecting others. You just have to make sure you fire the correct events.

It may be easier to extend DefaultTreeSelectionModel, since it provides and implementation for most of the event stuff.

bitguru blog
Maneesh Godbole
Saloon Keeper

Joined: Jul 26, 2007
Posts: 10451
    
    8

John Golt wrote:

Please check your private messages for an important administrative matter


[How to ask questions] [Donate a pint, save a life!] [Onff-turn it on!]
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: Custom Multiple Selection for JTree