0
package com.orb;

import java.util.LinkedList;
import javax.swing.table.AbstractTableModel;
import com.orb.bean.Product;

public class ProductTableModel extends AbstractTableModel {

    private static final long serialVersionUID = -250883760398754970L;
    private final LinkedList<Product> list= new LinkedList<Product>();
    private final LinkedList<Boolean> checkList = new LinkedList<Boolean>();
    public void addItem(Product customer) {
        list.add(customer);
        checkList.add(false);
        fireTableDataChanged();
    }

    @Override
    public int getColumnCount() {
            return 5;
    }

    @Override
    public int getRowCount() {
        return list.size();
    }

    @Override
    public Object getValueAt(int rowIndex, int columnIndex) {
        Object obj = null;

        if(columnIndex==4){
            setTotal(list.get(rowIndex));           
        }
        switch (columnIndex){
            case 0: obj= list.get(rowIndex).getCode() ;break;
            case 1: obj=list.get(rowIndex).getDescription(); break;
            case 2: obj=list.get(rowIndex).getQuantity();break;
            case 3: obj=list.get(rowIndex).getPrice();break;            
            case 4: obj=list.get(rowIndex).getTotal();break;            
        }
        return obj;
    }

    @Override
    public Class<?> getColumnClass(int arg0) {
        switch(arg0){
        case 0: case 1: return String.class; 
        case 2: return Integer.class; 
        case 3: case 4: return Double.class;
        }

        return super.getColumnClass(arg0);
    }
    @Override
    public boolean isCellEditable(int arg0, int arg1) {
        boolean isCellEditable = false;
        switch(arg1){
        case 2: case 3: isCellEditable= true;break;
        default: isCellEditable= false;break;
        }
        return isCellEditable;
        //return super.isCellEditable(arg0, arg1);
    }
    @Override
    public void setValueAt(Object arg0, int arg1, int arg2) {
        System.out.println("Value seted" +arg0 + arg1 + arg2);
        switch(arg2){
        case 0: break;
        case 1: break;
        case 2: list.get(arg1).setQuantity((Integer)arg0); setTotal(list.get(arg1)); break;
        case 3: list.get(arg1).setPrice((Double)arg0); setTotal(list.get(arg1));break;          
        case 4: list.get(arg1).setTotal((Double)arg0);break;

           //case 0: checkList.set(arg1, (Boolean)arg0);break;
           default:break;
        }
        //list.get(arg1).setTotal((Double)arg0);
        fireTableDataChanged();
    }

    public LinkedList<Product> getList() {
        LinkedList<Product> temp = new LinkedList<Product>();
        int index=-1;
        for(Boolean isSelected:checkList){
            index++;
            if(isSelected){
                temp.add(list.get(index));
            }
        }
        return temp;
    }

    public void setTotal(Product product){
        Double total = 0.0d;
        total = product.getQuantity ()* product.getPrice();
        product.setTotal(total);
    }
}

I have a class like above. I want to sum columns and put its total to a JTextfield. How can I do that?

Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
Ashish Mehta
  • 7,226
  • 4
  • 25
  • 52
  • 1
    The question for you is: why can't you do it yourself? What seems to be the problem? – Edwin Dalorzo Oct 19 '12 at 12:17
  • All you need to do is increase the number of rows by one (so return list.size()+1) and in the getValueAt(), check if the reequested rows is the last row, and if yes, return the total for the given column. – Guillaume Polet Oct 19 '12 at 12:21
  • @GuillaumePolet not sure whether that solution will work if you want to use the built-in row sorting – Robin Oct 19 '12 at 13:49
  • @Robin [no issue with that, is quite easy and possible], no progress on my side, never tried again , (https://forums.oracle.com/forums/thread.jspa?threadID=1349003&start=0&tstart=0), [I think that there you can to apply both methods sorter and filter too, sure with potential Bug (@ndrew Thompson) because works only for bridge based on String value ](http://stackoverflow.com/q/6187566/714968) – mKorbel Oct 19 '12 at 17:16

1 Answers1

2

have to

Community
  • 1
  • 1
mKorbel
  • 109,525
  • 20
  • 134
  • 319