0

My table model is as follows. The first coloumn of my table is checkbox. I am able to put checkbox in jtable but when I cliked on that checkbox it does nothing. I used the DefaultTableCellRenderer to put checkbox in my table.

public class MyTableModel implements TableModel {

    ArrayList<PersonDTO> list=new ArrayList<PersonDTO>();

    String headerName[]={"checkbox","student_id","name"};

    public MyTableModel(ArrayList<PersonDTO> list) {
        this.list=list;  
    }


    @Override
    public int getRowCount() {
        //throw new UnsupportedOperationException("Not supported yet.");
        System.out.println(list.size());
         return list.size();

    }

    @Override
    public int getColumnCount() {
        //throw new UnsupportedOperationException("Not supported yet.");
       // System.out.println(headerName.length);
        return headerName.length;
    }

    @Override
    public String getColumnName(int columnIndex) {
        //throw new UnsupportedOperationException("Not supported yet.");
       // System.out.println(headerName);
        return headerName[columnIndex];


    }

    @Override
    public Class<?> getColumnClass(int columnIndex) {
        //throw new UnsupportedOperationException("Not supported yet.");
        switch(columnIndex)
        {
            case 0:
                return JCheckBox.class;
                //return JCheckBox.class;
            case 1:
                return Integer.class;
            case 2:
                return String.class;


        }
        return null;
    }

    @Override
    public boolean isCellEditable(int rowIndex, int columnIndex) {
       // throw new UnsupportedOperationException("Not supported yet.");
         return  true;

    }

    @Override
    public Object getValueAt(int rowIndex, int columnIndex) {
        //throw new UnsupportedOperationException("Not supported yet.");
        PersonDTO personDTO=list.get(rowIndex);
         switch(columnIndex)
        {
            case 0:
               //System.out.println(personDTO.isCheckbox());
              return personDTO.getCheckBox();
                //return new JCheckBox();

            case 1:
                //System.out.println(personDTO.getName());
                return personDTO.getUserId();
            case 2:
                return personDTO.getName();


        }
        return null;
    }

    @Override
    public void setValueAt(Object aValue, int rowIndex, int columnIndex) {
        //throw new UnsupportedOperationException("Not supported yet."); 

    }

    @Override
    public void addTableModelListener(TableModelListener l) {
       // throw new UnsupportedOperationException("Not supported yet.");
    }

    @Override
    public void removeTableModelListener(TableModelListener l) {
        //throw new UnsupportedOperationException("Not supported yet.");
    }

}
Jay
  • 4,627
  • 1
  • 21
  • 30
  • Just check this link.. [1]: http://stackoverflow.com/questions/11328451/setting-jcheckbox-invisible-in-jtable – Soorya Thomas Oct 19 '12 at 09:42
  • You just check your jTable columns properties ,that columns are editable or not??...If field is non editable then that can changed into editable.Then you can most probably solved your problem. – Soorya Thomas Oct 19 '12 at 09:50

1 Answers1

1

Use Boolean class for the check box column.

public Class<?> getColumnClass(int columnIndex) {
switch(columnIndex)
{
    case 0:
        // return JCheckBox.class;
           return Boolean.class;
    case 1:
        return Integer.class;
    case 2:
        return String.class;
}
   return null;
}
Amarnath
  • 8,736
  • 10
  • 54
  • 81