Question

For some strange reason each time I edit data in any cell and confirm it Once I go to the next one it copies the data from previously edited cell

any ideas?

public class CheckoutTableModel extends DefaultTableModel {

    private String[] columnNames= {"Brand","Model","Price","Quantity","Total Price"};
    private List<Integer> id;
    private List<CheckoutItem> basketItems;
    private static final long serialVersionUID = 7944308974044321712L;


    public CheckoutTableModel()
    {
        id=new ArrayList<>();
        basketItems=new ArrayList<>();
    }

    public CheckoutTableModel(List<Item>db, ArrayList<Integer>quantity)
    {
        id=new ArrayList<>();
        basketItems=new ArrayList<>();
        for (int i = 0 ; i < db.size() ; i++)
        {
            basketItems.add(new CheckoutItem(db.get(i).getBrand(), db.get(i).getModel(), db.get(i).getPrice(), quantity.get(i)));
        }
    }

    public void setValueAt(Object value, int row, int column)
    {
        switch(column)
        {
        case 1: 
            basketItems.get(row).setBrand((String)value);
        case 2:
            basketItems.get(row).setModel((String)value);
        case 3:
            basketItems.get(row).setPrice((double)value);
        case 4:
            basketItems.get(row).setQuantity(Integer.parseInt((String) value));
        case 5:
            basketItems.get(row).setTotalPrice(Double.parseDouble((String) value));
        }
    }

    public int getRowCount() {
        if(basketItems==null)
        {
            return 0;
        }
        else
        return basketItems.size();
    }

    public int getColumnCount() {
        return 5;
    }

    public String getColumnName(int column) {
        return columnNames[column];
    }

    public Object getValueAt(int row, int column) {  //set values of cells
        switch(column)
        {
        case 0: 
            return basketItems.get(row).getBrand();
        case 1:
            return basketItems.get(row).getModel();
        case 2:
            return basketItems.get(row).getPrice();
        case 3:
            return basketItems.get(row).getQuantity();
        case 4:
            return basketItems.get(row).getTotalPrice();
        }
        return null;
    }

    public void setTableModel(List<Item>db, List<Integer>quantities){
        id=new ArrayList<>();
        basketItems=new ArrayList<>();
        for (int i = 0 ; i < db.size() ; i++)
        {
            basketItems.add(new CheckoutItem(db.get(i).getBrand(), db.get(i).getModel(), db.get(i).getPrice(), quantities.get(i)));
        }
    }

    public boolean isCellEditable(int row,int column)  
        {
            switch(column){             

           case 0:  // select the cell you want make it not editable 
             return false; 
           case 1:  // select the cell you want make it not editable 
               return false;
           case 2:
               return true;
           case 3:
               return true;
           case 4:
               return false;
         default: return false;}  
     }

     @SuppressWarnings("unchecked")
     public Class getColumnClass(int column) {
            return getValueAt(0, column).getClass();
     }
}
Was it helpful?

Solution

So there are at least two problems in your setValueAt method...

  1. Columns (and rows) are 0 indexed
  2. switch statements will allow for a case to be executed and all child cases below as well...

So, this means...

public void setValueAt(Object value, int row, int column)
{
    switch(column)
    {
    case 1: 
        basketItems.get(row).setBrand((String)value);
    case 2:
        basketItems.get(row).setModel((String)value);
    case 3:
        basketItems.get(row).setPrice((double)value);
    case 4:
        basketItems.get(row).setQuantity(Integer.parseInt((String) value));
    case 5:
        basketItems.get(row).setTotalPrice(Double.parseDouble((String) value));
    }
}

If column == 1, the cases 1, 2, 3, 4 and 5 will be executed. If column == 3, the cases 3, 4 and 5 will be executed.

So, first, you will want to modify the switch so that the cases match the same order as the getValueAt method and add a break statement after each case in order to prevent the following cases from been executed, for example...

public void setValueAt(Object value, int row, int column)
{
    switch(column)
    {
    case 0: 
        basketItems.get(row).setBrand((String)value);
        break;
    case 1:
        basketItems.get(row).setModel((String)value);
        break;
    case 2:
        basketItems.get(row).setPrice((double)value);
        break;
    case 3:
        basketItems.get(row).setQuantity(Integer.parseInt((String) value));
        break;
    case 4:
        basketItems.get(row).setTotalPrice(Double.parseDouble((String) value));
        break;
    }
}

OTHER TIPS

Sure, dumb mistake here:

public void setValueAt(Object value, int row, int column)
{
    switch(column)
    {
    case 1: 
        basketItems.get(row).setBrand((String)value);
    case 2:
        basketItems.get(row).setModel((String)value);
    case 3:
        basketItems.get(row).setPrice((double)value);
    case 4:
        basketItems.get(row).setQuantity(Integer.parseInt((String) value));
    case 5:
        basketItems.get(row).setTotalPrice(Double.parseDouble((String) value));
    }
}

should be, of course, from case 0 to case 4.

Even though I found the error just by looking at your code, it is not unusual to overlook little mistakes like these that are hard to find later when the code grows larger. You have to take a more methodical aproach to find these, use a debugger or simply put some outputs indicating the calls and parameters to the functions that you are looking at. This way, for example, you would quickly have seen that editing the second column would make a call to setBrand not to setModel as expected and you would have found the error right away.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top