title: Exception in thread "AWT-EventQueue-0" java.lang.ClassCastException: javax.swing.JTable
body: <p>I have a <a href="http://www.coderanch.com/t/343795/GUI/java/Check-Box-JTable-header" rel="nofollow">code</a> which when run generates a table in swing form which contains a set of checkboxes which can be selected or unselected </p>
  
  <p>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 :</p>
  
  <pre>
  > 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)
  </pre>
  
  <p>The <a href="http://www.coderanch.com/t/343795/GUI/java/Check-Box-JTable-header" rel="nofollow">code</a>:</p>
  
  <pre><code>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 &lt; 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 &lt; 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 &amp;&amp; e.getClickCount() == 1 &amp;&amp; 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()......");
      }
  }
  </code></pre>
  
  <p>with Exception</p>
  
  <pre><code>Exception in thread "AWT-EventQueue-0" java.lang.ClassCastException: 
  javax.swing.JTable cannot be cast to javax.swing.table.JTableHeader
  </code></pre>