2010-01-11 28 views
3

我的應用程序處理患者記錄。在主框架中,用戶可以打開幾個內部框架。每個內部框架都包含一個選項卡窗格,用戶創建的每個選項卡都包含一個可輸入患者數據的表單和一個顯示所有添加患者的jtable。java swing-轉義鍵事件導致classCastException的外觀

當用戶單擊jtable中的一行(患者)時,表格的字段將填入患者的數據,當他按下「Escape」時,表格的字段將被清除,用戶可以繼續搜索/檢查/輸入另一位患者。

我的問題是,這個轉義鍵事件,在我使用的Substance look and feel中拋出classCastException。我爲執行的操作編寫的代碼工作正常。自從我開始使用選項卡式窗格(在單個窗格中創建所有內容之前)出現了此問題。如果我改變外觀和感覺,例如Windows,則不會拋出異常。你有什麼主意嗎?

這裏是一個代碼示例:

private void db_existKeyReleased(java.awt.event.KeyEvent evt) { 
    // TODO add your handling code here: 

    if(evt.getKeyCode()==KeyEvent.VK_ESCAPE) 
    { 
     searchField.requestFocusInWindow();   // if user doesn't want to process any of the entries shown to the table 
     if(searchField.getText().length()>=1)  // focus goes to search field and data pane fields are cleared form previous shows 
     { 
      dataPane.setPid(-1); 
      dataPane.getPersonalDataPane().clearAll(); 
      treat_diagPane.getDiagnosis_pane().clearAll(); 
      treat_diagPane.getTreat_pane().clearAll(); 
     } 

     DefaultTableModel model=new DefaultTableModel(
       new Object [][] { 
     }, 
     new String [] { 
      bundle.getString("lname"), bundle.getString("fname"), bundle.getString("date_birth"), bundle.getString("occupation") 
     }); 
     db_exist.setModel(model); 
    } 

這是拋出的異常:

Exception in thread "AWT-EventQueue-0" java.lang.ClassCastException: javax.swing.JTabbedPane cannot be cast to javax.swing.JDesktopPane 
    at javax.swing.plaf.basic.BasicDesktopPaneUI$Actions.actionPerformed(BasicDesktopPaneUI.java:329) 
    at org.jvnet.lafwidget.tabbed.TabPagerWidget$4.actionPerformed(TabPagerWidget.java:158) 

,這是導致異常的代碼:

public void actionPerformed(ActionEvent e) { 
     JDesktopPane dp = (JDesktopPane)e.getSource(); 
     String key = getName(); 

     if (CLOSE == key || MAXIMIZE == key || MINIMIZE == key || 
       RESTORE == key) { 
      setState(dp, key); 
     } 
     else if (ESCAPE == key) { 
      if (sourceFrame == dp.getSelectedFrame() && 
        focusOwner != null) { 
       focusOwner.requestFocus(); 
      } 
      moving = false; 
      resizing = false; 
      sourceFrame = null; 
      focusOwner = null; 
     } 
+0

嗨,歡迎計算器!提示:可以發佈很長的問題。爲了使它們可讀,請使用空行來創建新段落。我冒昧地編輯你的問題並添加它們:-)。 – sleske 2010-01-11 11:33:53

+0

另外,請鏈接到您使用的外部軟件,除非它很常見(如Eclipse或Apache共享)。我將「物質L&F」與你聯繫起來。 – sleske 2010-01-11 11:34:56

回答

2

您提你切換到使用JTabbedPane,但在你的「actionPerformed」方法代碼中,你仍然投射到「JDesktopPane」和堆棧跟蹤說:

Exception in thread "AWT-EventQueue-0" java.lang.ClassCastException: 
javax.swing.JTabbedPane cannot be cast to javax.swing.JDesktopPane at 
javax.swing.plaf.basic.BasicDesktopPaneUI$Actions.actionPerformed 

可能將其更改爲:

if(e.getSource() instanceof JTabbedPane) { 
    JTabbedPane = (JTabbedPane)e.getSource(); 
} 

可能是你需要做的改變。

+0

你是非常正確的,但不幸的是導致異常的代碼不是我的,而是實質的外觀和感覺,不幸的是我不能改變它,如果我能我不知道是否會導致其他問題。有些外觀和感覺可能不適合某些應用程序「運行」嗎?我應該改變外觀和感覺嗎?非常感謝您的即時答覆!在這裏真棒! – gosling 2010-01-11 11:42:58

+0

對不起,我不知道'物質'的外觀和感覺,我已經發布了我的答案完全看着堆棧跟蹤,並假設你可以在actionPerformed方法中修改代碼。我希望Substance LF的知名用戶能夠提供一種適合您的解決方案。 – sateesh 2010-01-11 11:56:41

0

Use keybindings

this.getRootPane().getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW).put(KeyStroke.getKeyStroke(KeyEvent.VK_ESCAPE, 0), "fecharAction"); 
    this.getRootPane().getActionMap().put("fecharAction", new AbstractAction() { 
     private static final long serialVersionUID = 1L; 
     @Override 
     public void actionPerformed(ActionEvent e) { 
      int resp = JOptionPane.showConfirmDialog(MainForm.this, "Encerrar sistema?", "Confirmação", JOptionPane.YES_NO_OPTION, JOptionPane.QUESTION_MESSAGE); 
      if (resp == 0) { 
       MainForm.this.setVisible(false); 
       MainForm.this.dispose(); 
      } 
     } 
    });