jeudi 7 janvier 2016

How to make checkbox in AbstractTableModel working

Need help i have a problem using AbstractTableModel , i want to add checkbox in cell JTable but i am getting this error :

SEVERE: null
java.lang.NullPointerException
    at com.java.pps.eveshop.jsonModel.ListTrxTableModel.getColumnClass(ListTrxTableModel.java:102)
    at javax.swing.JTable.getColumnClass(JTable.java:2701)
    at javax.swing.JTable.getCellRenderer(JTable.java:5686)

i know the error message is in here :

@SuppressWarnings({"unchecked", "rawtypes"})
    @Override
    public Class getColumnClass(int column) {
        if (column == 11) {
            return Boolean.class;
        }
        return (getValueAt(0, column).getClass());
    }

but i dont know how to fix it, when i delete the code above i just getting false value as a string not a checkbox. Here is my full code AbstractTableModel class :

public class ListTrxTableModel extends AbstractTableModel {
    private List<Boolean> rowList;
    private Set<Integer> checked = new TreeSet<Integer>();
    private final List<ListTrxModel> listTrx;
//    private final String[] columns;
    private final String[] columns = new String[]{"Nomor", "User name", "Request date", "Execute date", "MSISDN", "Voucher Code", "Nominal", "Sales_Price", "Status", "Mac address", "No truk", "Selected"};
    public ListTrxTableModel(List<ListTrxModel> ListTrx) {
        super();
        listTrx = ListTrx;
        rowList = new ArrayList<Boolean>(ListTrx.size());
        for (int i = 0; i < ListTrx.size(); i++) {
        rowList.add(Boolean.FALSE);
        }   
    }
    @Override
    public int getColumnCount() {
        return columns.length;
    }

    // Number of row of your table
    public int getRowsCount() {
        return listTrx.size();
    }

    @Override
    public Object getValueAt(int row, int col) {
        ListTrxModel trx = listTrx.get(row);
        switch (col) {
            case 0:
                return trx.getId();
            case 1:
                return trx.getUser_name();
            case 2:
                return trx.getRequest_date();
            case 3:
                return trx.getExecute_date();
            case 4:
                return trx.getMsisdn();
            case 5:
                return trx.getKode_voucher();
            case 6:
                return trx.getNominal();
            case 7:
                return trx.getSales_price();
            case 8:
                return trx.getStatus();
            case 9:
                return trx.getMacaddress();
            case 10:
                return trx.getNostruk();
            case 11:
                return rowList.get(row).booleanValue();
            default:
                return null;
        } //To change body of generated methods, choose Tools | Templates.
    }

    @Override
    public String getColumnName(int col) {
        return columns[col];
    }

  @Override
  public boolean isCellEditable(int row, int col) {   
  if (col < 1) {    
      return false;  
  } 
  else if( col == 11)
  {
      return true;
  }
  else {
      return false;   
  }

}


    @SuppressWarnings({"unchecked", "rawtypes"})
    @Override
    public Class getColumnClass(int column) {
        if (column == 11) {
            return Boolean.class;
        }
        return (getValueAt(0, column).getClass());
    }

    @Override
    public int getRowCount() {
        return listTrx.size(); //To change body of generated methods, choose Tools | Templates.
    }
   @Override
        public void setValueAt(Object aValue, int row, int col) {
            boolean b = (Boolean) aValue;
            rowList.set(row, b);
            if (b) {
                checked.add(row);
            } else {
                checked.remove(row);
            }
            fireTableRowsUpdated(row, row);
        }

}

please help me, thank you.




Aucun commentaire:

Enregistrer un commentaire