简体   繁体   English

DefaultTableModel数据从先前编辑的单元格移动

[英]DefaultTableModel data moves from previously edited cell

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();
     }
}

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 . 当然,应该是从case 0case 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. 这样,例如,您很快就会看到编辑第二列会调用setBrand而不是按预期调用setModel ,您可能会立即找到错误。

So there are at least two problems in your setValueAt method... 所以你的setValueAt方法至少有两个问题......

  1. Columns (and rows) are 0 indexed 列(和行)为0索引
  2. switch statements will allow for a case to be executed and all child cases below as well... switch语句将允许执行一个案例以及下面的所有子案例......

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. 如果column == 1 ,则将执行情况1,2,3,4和5。 If column == 3 , the cases 3, 4 and 5 will be executed. 如果column == 3 ,则将执行情况3,4和5。

So, first, you will want to modify the switch so that the case s match the same order as the getValueAt method and add a break statement after each case in order to prevent the following case s from been executed, for example... 因此,首先,您需要修改switch以便case s匹配与getValueAt方法相同的顺序,并在每个case之后添加break语句,以防止执行以下case ,例如......

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;
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM