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(selecte dNode + " 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.


More...