我試圖在我的應用程序中添加一個通用的右鍵單擊文本框。我遇到了一個解決方案,我可以在框架頂部添加一個玻璃窗格,使其不可見並將其註冊爲通用鼠標監聽器。如果組件是文本字段,則顯示彈出式菜單,否則我將重新分派該事件。我粘貼下面的代碼...當我添加一個glasspane到我的框架時,出現stackoverflow錯誤
這個例子工作正常。但是,當我使用這個我的申請,雖然,我在
Exception in thread "AWT-EventQueue-0" java.lang.StackOverflowError
at apple.awt.CWindow._getLocationOnScreen(Native Method)
at apple.awt.CWindow.getLocationOnScreen(CWindow.java:878)
at java.awt.Component.getLocationOnScreen_NoTreeLock(Component.java:1960)
at java.awt.Component.getLocationOnScreen(Component.java:1938)
at javax.swing.SwingUtilities.convertPointToScreen(SwingUtilities.java:364)
at javax.swing.SwingUtilities.convertPoint(SwingUtilities.java:165)
at com.aesthete.csmart.ui.common.components.RightClickGlassPane.redispatchMouseEvent(RightClickGlassPane.java:79)
at com.aesthete.csmart.ui.common.components.RightClickGlassPane.mouseEntered(RightClickGlassPane.java:61)
得到一個計算器的錯誤,我明白,每次進入玻璃嵌板接收到該事件的組件上的鼠標,然後時間重新調度。但爲什麼它被轉換成遞歸調用?
編輯: 只是想告訴大家我是如何與Camickr建議解決了這個問題:
SwingUtilities.invokeLater(new Runnable() {
public void run() {
final JPopupMenu popup = new JPopupMenu();
JMenuItem mnItemCopy = new JMenuItem("Copy", CommonUI.getScaledImage(13, 13, "/images/copy.png"));
JMenuItem mnItemCut = new JMenuItem("Cut", CommonUI.getScaledImage(13, 13, "/images/cut.png"));
JMenuItem mnItemPaste = new JMenuItem("Paste", CommonUI.getScaledImage(13, 13, "/images/paste.png"));
popup.add(mnItemCopy);
popup.add(mnItemCut);
popup.add(mnItemPaste);
Toolkit.getDefaultToolkit().addAWTEventListener(new AWTEventListener() {
@Override
public void eventDispatched(AWTEvent event) {
if(event instanceof MouseEvent) {
MouseEvent mouseevent=(MouseEvent)event;
if(mouseevent.isPopupTrigger()) {
if (mouseevent.getComponent() instanceof JTextField) {
popup.show(mouseevent.getComponent(), mouseevent.getX(), mouseevent.getY());
}
}
}
}
}, AWTEvent.MOUSE_EVENT_MASK);
}
});
爲了更快提供更好的幫助,請發佈[SSCCE](http://sscce.org/)。請注意,根據未顯示問題的代碼,現在有** 2個已刪除的帖子!現在已經編輯了相同的代碼,以避免進一步的混淆。 – 2011-12-23 03:56:01
謝謝安德魯。抱歉,關於不正確的SSCCE。我使用了我在代碼庫中發佈的同一個類。我解壓出來,看到它正確運行。我不確定爲什麼在與我的代碼庫集成時失敗。我想我會把我所有的信息。 – sethu 2011-12-23 05:22:27
很高興你把它分類。反向投票 - 報告解決方案。 – 2011-12-23 05:26:36