我使用鼠標偵聽器來了解用戶何時單擊JTree的節點。當用戶點擊一個節點的擴展箭頭儘管(查看孩子的)以下異常被拋出:MouseListener和JTree
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)
峽聽衆:
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);
任何建議,我應該如何處理這種情況?我是否應該簡單地在if語句裏面試一下?此外,這是一個檢查雙擊的好方法,或者我應該用不同的方法來做到這一點?謝謝
我檢查了這個監聽程序並實現了它,但是我找不到如何檢查雙擊。是甚至可能的嗎? – Giannis 2011-12-27 12:03:50
錯過了那一點,對不起。 – 2011-12-27 12:52:56