本文介绍了线程中的异常“AWT-EventQueue-0” java.lang.ClassCastException:javax.swing.JTable的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个,当时运行生成一个摆动表格,它包含一组可以选择或未选择的复选框。

I have a code which when run generates a table in swing form which contains a set of checkboxes which can be selected or unselected

当我点击检查所有选项卡时,我可以选择/取消选择所有下面的其他复选框,但是当我单独选择以下复选框之一时,我会收到此错误:

When I click on the Check All tab I am able to select/unselect all the other below check boxes but when i select one of the below checkboxes individually I get this error :


> Exception in thread "AWT-EventQueue-0" java.lang.ClassCastException: javax.swing.JTable
    at com.tps.charts.CheckBoxHeader.handleClickEvent(JTableHeaderCheckBox.java:152)
    at com.tps.charts.CheckBoxHeader.mouseClicked(JTableHeaderCheckBox.java:168)
    at java.awt.AWTEventMulticaster.mouseClicked(Unknown Source)
    at java.awt.AWTEventMulticaster.mouseClicked(Unknown Source)
    at java.awt.AWTEventMulticaster.mouseClicked(Unknown Source)
    at java.awt.AWTEventMulticaster.mouseClicked(Unknown Source)
    at java.awt.AWTEventMulticaster.mouseClicked(Unknown Source) mousePressed......
    at java.awt.AWTEventMulticaster.mouseClicked(Unknown Source)
    at java.awt.AWTEventMulticaster.mouseClicked(Unknown Source)
    at java.awt.AWTEventMulticaster.mouseClicked(Unknown Source)
    at java.awt.Component.processMouseEvent(Unknown Source)
    at javax.swing.JComponent.processMouseEvent(Unknown Source)
    at java.awt.Component.processEvent(Unknown Source)
    at java.awt.Container.processEvent(Unknown Source)
    at java.awt.Component.dispatchEventImpl(Unknown Source)
    at java.awt.Container.dispatchEventImpl(Unknown Source)
    at java.awt.Component.dispatchEvent(Unknown Source)
    at java.awt.LightweightDispatcher.retargetMouseEvent(Unknown Source)
    at java.awt.LightweightDispatcher.processMouseEvent(Unknown Source)
    at java.awt.LightweightDispatcher.dispatchEvent(Unknown Source)
    at java.awt.Container.dispatchEventImpl(Unknown Source)
    at java.awt.Window.dispatchEventImpl(Unknown Source)
    at java.awt.Component.dispatchEvent(Unknown Source)
    at java.awt.EventQueue.dispatchEvent(Unknown Source)
    at java.awt.EventDispatchThread.pumpOneEventForHierarchy(Unknown Source)
    at java.awt.EventDispatchThread.pumpEventsForHierarchy(Unknown Source)
    at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
    at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
    at java.awt.EventDispatchThread.run(Unknown Source)

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.table.*;

public class JTableHeaderCheckBox {

    private Object colNames[] = {"", "String", "String"};
    private Object[][] data = {};
    private DefaultTableModel dtm;
    private JTable table;
    private TableColumn tc;

    public void buildGUI() {
        dtm = new DefaultTableModel(data, colNames);
        table = new JTable(dtm);
        for (int x = 0; x < 5; x++) {
            dtm.addRow(new Object[]{false, "Row " + (x + 1) + " Col 2", "Row " + (x + 1) + " Col 3"});
        }
        JScrollPane sp = new JScrollPane(table);
        tc = table.getColumnModel().getColumn(0);
        tc.setCellEditor(table.getDefaultEditor(Boolean.class));
        tc.setCellRenderer(table.getDefaultRenderer(Boolean.class));
        tc.setHeaderRenderer(new CheckBoxHeader(new MyItemListener()));
        JFrame f = new JFrame();
        f.getContentPane().add(sp);
        f.pack();
        f.setLocationRelativeTo(null);
        f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        f.setVisible(true);
    }

    private class MyItemListener implements ItemListener {

        @Override
        public void itemStateChanged(ItemEvent e) {
            System.out.println("ItemStateChanged");
            Object source = e.getSource();
            if (source instanceof AbstractButton == false) {
                return;
            }
            boolean checked = e.getStateChange() == ItemEvent.SELECTED;
            for (int x = 0, y = table.getRowCount(); x < y; x++) {
                table.setValueAt(checked, x, 0);
            }
        }
    }

    public static void main(String[] args) {
        SwingUtilities.invokeLater(new Runnable() {

            @Override
            public void run() {
                new JTableHeaderCheckBox().buildGUI();
            }
        });
    }
}

class CheckBoxHeader extends JCheckBox implements TableCellRenderer, MouseListener {

    private static final long serialVersionUID = 1L;
    private CheckBoxHeader rendererComponent;
    private int column;
    private boolean mousePressed = false;

    public CheckBoxHeader(ItemListener itemListener) {
        rendererComponent = this;
        rendererComponent.addItemListener(itemListener);
    }

    @Override
    public Component getTableCellRendererComponent(JTable table, Object value,
            boolean isSelected, boolean hasFocus, int row, int column) {
        if (table != null) {
            JTableHeader header = table.getTableHeader();
            table.addMouseListener(rendererComponent);
            if (header != null) {
                rendererComponent.setForeground(header.getForeground());
                rendererComponent.setBackground(header.getBackground());
                rendererComponent.setFont(header.getFont());
                header.addMouseListener(rendererComponent);
            }
        }
        setColumn(column);
        rendererComponent.setText("Check All");
        setBorder(UIManager.getBorder("TableHeader.cellBorder"));
        return rendererComponent;
    }

    protected void setColumn(int column) {
        this.column = column;
    }

    public int getColumn() {
        return column;
    }

    protected void handleClickEvent(MouseEvent e) {
        if (mousePressed) {
            mousePressed = false;
            JTableHeader header = (JTableHeader) (e.getSource());
            JTable tableView = header.getTable();
            TableColumnModel columnModel = tableView.getColumnModel();
            int viewColumn = columnModel.getColumnIndexAtX(e.getX());
            column = tableView.convertColumnIndexToModel(viewColumn);
            if (viewColumn == this.column && e.getClickCount() == 1 && column != -1) {
                System.out.println(" doClick()......");
                doClick();
            }
        }
    }

    @Override
    public void mouseClicked(MouseEvent e) {
        System.out.println(" mouseClicked()......");
        handleClickEvent(e);
        /* problem occurs from this line */
        ((JTableHeader) e.getSource()).repaint();
    }

    @Override
    public void mousePressed(MouseEvent e) {
        //System.out.println("mousePressed(MouseEvent e).......");
        mousePressed = true;
    }

    @Override
    public void mouseReleased(MouseEvent e) {
        //System.out.println(" mouseReleased()......");
    }

    @Override
    public void mouseEntered(MouseEvent e) {
        //System.out.println(" mouseEntered()......");
    }

    @Override
    public void mouseExited(MouseEvent e) {
        //System.out.println("mouseExited()......");
    }
}

与异常

Exception in thread "AWT-EventQueue-0" java.lang.ClassCastException:
javax.swing.JTable cannot be cast to javax.swing.table.JTableHeader


推荐答案

您正在将事件的来源投射到 JTableHeader

You are casting the source of the event to a JTableHeader:

JTableHeader header = (JTableHeader) (e.getSource());

,而源是一个 JTable 点击一个单元格。我将为标题和单元格选择有两个不同的侦听器,或者您可以通过检查 event.getSource()instanceof ... 在您的鼠标点击监听器。

and the source is a JTable when you click at a cell. I would have two different listeners for the header and the cell selection or you can do a hack by checking event.getSource() instanceof ... in your mouse-click-listener.

这篇关于线程中的异常“AWT-EventQueue-0” java.lang.ClassCastException:javax.swing.JTable的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-23 12:22