开发者

java swing- escape key event causes classCastException in look and feel

My application processes patient records. In the main frame user can open several internal frames. Each internal frame contains a tabbed pane and each tab created by the user contains a form where he can enter patient's data and a jtable where all added patients are shown.

When the user clicks a row(patient) in the jtable the form's fields is filled by patient's data and when he presses "Escape" the form's fields are cleared and the user can go on to search/check/enter another patient.

My problem is that this escape key event, throws a classCastException in the Substance look and feel i use. The code I've written for the action performed works fine. This problem appeared since i started to use tabbed panes (before everything was made in a single pane). If i change look and feel to e.g.Windows no exception is thrown. Do you have any idea?

Here is a sample of code:

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);
    }

This is the exception thrown :

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)

and this is the code that causes the exception:

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();
            }
            mo开发者_运维技巧ving = false;
            resizing = false;
            sourceFrame = null;
            focusOwner = null;
        }


You mention that you switched to using JTabbedPane, but in your "actionPerformed" method code you still cast to "JDesktopPane" and the stacktrace says:

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

Probably changing it to:

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

might be the change you need to do.


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();
            }
        }
    });
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜