• Post Reply Bookmark Topic Watch Topic
  • New Topic

Mapping Keys to nodes in JTree

 
Sajee Joseph
Ranch Hand
Posts: 200
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
I am developing a Chat application. In this the users are shown in a tree.How do i map keys to these nodes such that when i press a key, focus goes to the node that begins with that letter.
For instance, if i press 'J', focus should go to the node which starts with 'J'( Joseph for instance). If there are more than one node starting with 'J', let focus go to the first such node.
Please help me out. Code examples will be very helpful
...thanks
...Saj
 
Luong Nguyen
Ranch Hand
Posts: 31
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
I think you should implement a KeyListener for your tree. In this listener, when a key is pressed you must find the node that begins with that letter and get a TreePath accord with the node. Then you set selected node by the TreePath. However, it isn't possible work if you use addKeyListener(KeyListener) method to add listener for the tree. The JTree has a class (BasicTreeUI) that is used to render and handle events for the tree. In this class has an inner class that implements KeyListener and you can create your class that is extended from BasicTreeUI. In your class, you should modify the inner class to handle things.
Originally posted by Sajee Joseph:
Hi,
I am developing a Chat application. In this the users are shown in a tree.How do i map keys to these nodes such that when i press a key, focus goes to the node that begins with that letter.
For instance, if i press 'J', focus should go to the node which starts with 'J'( Joseph for instance). If there are more than one node starting with 'J', let focus go to the first such node.
Please help me out. Code examples will be very helpful
...thanks
...Saj

 
Luong Nguyen
Ranch Hand
Posts: 31
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
I think you should implement a KeyListener for your tree. In this listener, when a key is pressed you must find the node that begins with that letter and get a TreePath accord with the node. Then you set selected node by the TreePath. However, it isn't possible work if you use addKeyListener(KeyListener) method to add listener for the tree. The JTree has a class (BasicTreeUI) that is used to render and handle events for the tree. In this class has an inner class that implements KeyListener and you can create your class that is extended from BasicTreeUI. In your class, you should modify the inner class to handle things.
Please refer to BasicTreeUI class.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!