Java swing-escape key event raises classCastException in image and feel

My applications process patient records. In the main frame, the user can open several internal frames. Each internal frame contains a tabbed panel, and each tab created by the user contains a form in which he can enter patient data and jtable, which shows all the added patients.

When the user clicks on the row (patient) in the jtable, the form fields are filled in with the patient data, and when he presses "Escape", the form fields are cleared and the user can continue searching / checking / entering another patient.

My problem is that this escape key event throws a classCastException in the Appearance of the sensation I'm using. The code I wrote for the action performed works fine. This problem has occurred since I started using tabbed panels (before everything was done in one panel). If I change the appearance, for example, on Windows, there will be no exception. Do you have any ideas?

Here is a sample 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 an exception:

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 throws 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();
            }
            moving = false;
            resizing = false;
            sourceFrame = null;
            focusOwner = null;
        }
+3
source share
2 answers

, JTabbedPane, actionPerformed "JDesktopPane", stacktrace :

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

>

, .

+2

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

Source: https://habr.com/ru/post/1728026/


All Articles