0

我的表模型如下。我的表格的第一列是复选框。我可以将复选框放在 jtable 中,但是当我单击该复选框时,它什么也不做。我使用DefaultTableCellRenderer将复选框放在我的表格中。

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.");
    }

}
4

1 回答 1

1

对复选框列使用布尔类。

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;
}
于 2012-10-19T13:47:53.420 回答