X

Geertjan's Blog

  • September 22, 2009

Listening to Key & Mouse Events on BeanTreeViews

Geertjan Wielenga
Product Manager
Several NetBeans Platform developers understandably want to listen to mouse events and key events on their BeanTreeViews. Here's how, in the case where you want to listen to key presses. In other words, extend BeanTreeView and then use the event handler of your choice as shown below...
public class MyBTV extends BeanTreeView {
public MyBTV() {
tree.addKeyListener(new KeyAdapter() {
@Override
public void keyPressed(KeyEvent e) {//Find the ExplorerManager for this explorer view:
ExplorerManager mgr = ExplorerManager.find(MyBTV.this);//Get the selected node from the ExplorerManager:
String selectedNode = mgr.getSelectedNodes()[0].getDisplayName();//Get the pressed key from the event:
String pressedKey = e.getKeyText(e.getKeyCode());//Put a message in the status bar:
StatusDisplayer.getDefault().setStatusText(selectedNode +
" is being pressed by the " + pressedKey + " key!");
}
});
}
}

And what's "tree" in line 3 above? According to the BeanTreeView Javadoc, that's a field inherited from org.openide.explorer.view.TreeView, where it represents the main JTree component.

So, now you have your own BeanTreeView, which you can use just like the default BeanTreeView that comes with the NetBeans Platform.

It's the JTree, not the Node, that's been pressed. However, the point is, now you can find out what key event (or mouse event) is taking place when a particular Node is selected. So, if the user presses Enter, maybe something will happen, such as the display name of the selected Node being put somewhere else in the application (in a shopping basket of some kind, e.g., a list of chosen videos or employees), or whatever. Knowing the current key press as well as the selected Node can be a very useful thing. For example, this is one way of knowing whether a Node is being expanded/collapsed, since that's the Left/Right keys on the keyboard.

In other news. If you haven't taken a moment to listen to Jaroslav Tulach's presentation on MVC on the NetBeans Platform, published (and recorded) today here on netbeans.dzone.com, you need to listen to it right now, especially if you're using the NetBeans Platform! And vote for it, if you like it, of course.

Join the discussion

Comments ( 4 )
  • guest Saturday, February 8, 2014

    Hi Geertjan,

    the keyevents of BeanTreeViews seems to be mysteric. How could you catch the keyevents from some keys. If you press a key normally a node which begins with this letter is selected. How can I intercept this? Switch of with setDefaultActionAllowed(false) and setQuickSearchAllowed(false) doesn't help.

    Thanks in advance

    Karsten


  • Geertjan Saturday, February 8, 2014

    But... doesn't this blog entry explain exactly how to do that..?


  • guest Monday, February 10, 2014

    Regrettably, not. If is given in the above example "T", the node Tom is selected. I would just like to prevent this.


  • Geertjan Monday, February 10, 2014

    No idea what you mean, nor why. Just drop me an e-mail at geertjan dot wielenga at oracle dot com and please explain what it is you're trying to do.


Please enter your name.Please provide a valid email address.Please enter a comment.CAPTCHA challenge response provided was incorrect. Please try again.