1 Reply Latest reply: Dec 18, 2013 3:25 PM by TPD-Opitz RSS

    “AWT-EventQueue-0” java.lang.ClassCastException

    Baddy2407

      My main objective is to add checkboxes to a column in the jtable. But, when I am trying to return the Boolean value for that cell, it is throwing { Exception in thread "AWT-EventQueue-0" java.lang.ClassCastException: com.ciber.ecc.ui.results.CellInfo incompatible with java.lang.Boolean}

      My classes: DocumentTable.java

      public DocumentTable()
        {
      super();
      this.setSelectionBackground(new Color(0x33, 0xcc, 0x33));
        this.setSelectionForeground(Color.black);
      this.setRowSelectionAllowed(true);
        this.invalidate();
      this.validate();
      this.repaint();
        }
      public void initPropMenu() {
        String[] props = this.model.getProperties();
        TableColumnModel colModel = this.getColumnModel();
        colModel.addColumnModelListener(this.model);
        for (int i = 0; i < props.length; i++)
      {
      JCheckBoxMenuItem item = new JCheckBoxMenuItem(props[i]);
        int index = this.model.getIndex(props[i]);
      item.setSelected(false);
        TableColumn col = null;
        if(index ==0){
        col = new TableColumn(index);
        final JCheckBox checkBox = new JCheckBox();
        col.setCellRenderer(new DefaultTableCellRenderer() {
        public Component getTableCellRendererComponent(JTable table, Object value,
      boolean isSelected, boolean hasFocus, int row, int column)
      { checkBox.setSelected(((Boolean)value).booleanValue()) ;
        return checkBox;
        }
        });

      Cell Info.java

      public class CellInfo { 
      final private String text;
      final private int row;
        final private int column;
        final private ImageIcon icon;
        final private ImageIcon checked;
        public CellInfo(String text, ImageIcon icon, int row, int column, ImageIcon checked) {
      super();
      this.text = text;
      this.icon = icon;
        this.row = row;
      this.column = column;
      this.checked = checked;
        }
      public ImageIcon getIcon() {
        return icon;
      }
        public int getRow() {
        return row;
      }
      public String getText() {
        return text;
      }
        public int getColumn()
      { return column;
        }
      public ImageIcon getChecked() {
        return checked;
      }
        } 

      DocTableModel.java

       

      public class DocTableModel extends AbstractTableModel implements TableColumnModelListener
      {

      super();
      }

      public Object getValueAt(int rowIndex, int columnIndex)
      {
      String text = ((String[]) this.cols.get(columnIndex))[rowIndex];
      ImageIcon icon = data.getIcon(rowIndex);
      ImageIcon checked = null;

      if (data.getDocument(rowIndex).getIsCheckedOut())
      {
        checked = this.checkIcon;
      }

      return new CellInfo(text, icon, rowIndex, columnIndex, checked);

      }

      public boolean isCellEditable(int rowIndex, int columnIndex)
      {
      return columnIndex == 0;

      }

       

      The error is thrown at this line:

      checkBox.setSelected(((Boolean)value).booleanValue()) ;  
        • 1. Re: “AWT-EventQueue-0” java.lang.ClassCastException
          TPD-Opitz

          I'm missing your override of getColumnClass() like here:

           

          public class BooleanTableTest {
              private static final boolean[] test = new boolean[] { false, true, false,
                      true, false, true, false, true, };
          
              public static void main(String[] args) {
                  JTable jTable = new JTable(new AbstractTableModel() {
          
                      @Override
                      public Class<?> getColumnClass(int columnIndex) {
                          return columnIndex == 1 ? Boolean.class : String.class; // replace 'String' with your CellInfo and '1' with your boolean column index
                      }
          
                      @Override
                      public Object getValueAt(int arg0, int arg1) {
                          return arg1 == 1 ? Boolean.valueOf(test[arg0]) : "Cell" + "_"
                                  + String.valueOf(arg0) + "_" + String.valueOf(arg1);
                      }
          
                     
                      @Override
                      public boolean isCellEditable(int rowIndex, int columnIndex) {
                          return 1==columnIndex;
                      }
          
                      @Override
                      public void setValueAt(Object aValue, int rowIndex, int columnIndex) {
                          if (1 == columnIndex) {
                              test[rowIndex] = ((Boolean) aValue).booleanValue();
                          }
                          super.setValueAt(aValue, rowIndex, columnIndex);
                      }
          
                      @Override
                      public int getRowCount() {
                          return test.length;
                      }
          
                      @Override
                      public int getColumnCount() {
                          // TODO Auto-generated method stub
                          return 3;
                      }
                  });
                  JOptionPane.showMessageDialog(null, jTable);
              }
          
          }
          

          bye

          TPD