I use a mouse listener to find out when a user clicks on JTree nodes. Although, when the user clicks on the arrow to expand the node (view child elements), the following exception is thrown:
Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException at Core.ChannelView$1.mousePressed(ChannelView.java:120) at java.awt.AWTEventMulticaster.mousePressed(AWTEventMulticaster.java:263) at java.awt.Component.processMouseEvent(Component.java:6370) at javax.swing.JComponent.processMouseEvent(JComponent.java:3267)
ChannelView Listener:
MouseListener ml = new MouseAdapter() { public void mousePressed(MouseEvent e) { TreePath selPath = tree.getPathForLocation(e.getX(), e.getY()); if (e.getClickCount() == 1) { line 120>>>>> System.out.println(selPath.getLastPathComponent()); } else if (e.getClickCount() == 2) { System.out.println("Double" +selPath.getLastPathComponent()); } } }; tree.addMouseListener(ml);
Any suggestions on how I should handle this case? Should I just try to catch an if statement inside? Is this also a good way to check for double clicks, or should I do it with another method? Thanks
source share