编辑时,Jtable不会保存单元格的数据

时间:2016-08-21 17:41:36

标签: java swing jtable abstracttablemodel

我有一个可编辑列的JTable。它应该获得Integer值和更新表。但是当我编辑一个单元格并转到另一个单元格时,数据将被擦除并返回null,但程序不会抛出任何异常。我该如何解决?

public class FoodListPanel extends JPanel{
JTable jTable;
FoodTableModel fm;

public FoodListPanel() {

    try {
         fm = new FoodTableModel(FoodDAO.getAllFoodsFromDB(), new ArrayList<Integer>());

        jTable = new JTable(fm) {

            public boolean isCellEditable(int data, int columns) {
               if(columns<5){
                   return false;
               }
               else if(columns ==5){
                   return true;
               }
                else if(columns ==6){

                  if(getValueAt(data, 5)==Boolean.FALSE){
                      return false;
                  }
                   else {
                      return true;
                  }
               }
                else{
                   return true;
               }
            }

            public Component prepareRenderer(TableCellRenderer r, int data, int columns) {
                Component c = super.prepareRenderer(r, data, columns);
                return c;
            }


        };


        jTable.setPreferredScrollableViewportSize(new Dimension(650, 420));
        jTable.setFillsViewportHeight(true);

        JScrollPane jScrollPane = new JScrollPane(jTable);

        add(jScrollPane);

    } catch (SQLException e) {
        e.printStackTrace();
    }
}

class FoodTableModel extends AbstractTableModel {
    protected String[] cols = {"نام‌غذا", "دسته‌بندی", "قیمت", "توضیحات", "عکس" , "تعداد"};
    protected Class[] colClasses = {String.class, Integer.class, String.class, String.class,
    JLabel.class, Integer.class};
    ArrayList<Food> al;
    ArrayList<Integer> vals;
    public FoodTableModel(ArrayList<Food> foods, ArrayList<Integer> val) {
        al = new ArrayList<Food>();
        al.addAll(foods);


        vals = new ArrayList<Integer>(al.size());

        vals.addAll(val);

    }


    ////// TODO: 8/20/16 make dynamic from DB
    public int getColumnCount (){return cols.length;}
    public int getRowCount (){return al.size();}
    public String getColumnName(int col) { return cols[col]; }
    public Class getColumnClass(int col) { return colClasses[col]; }

    public Object getValueAt(int row, int col){
        switch (col) {

            case 0:
                return al.get(row).getName();
            case 1:
                return al.get(row).getType();
            case 2:
                return al.get(row).getPrice();
            case 3:
                return al.get(row).getDiscreption();
            case 5:
                if(vals.size()>= al.size()) return vals.get(row);

            default:
                return null;
        }
    }

    ////https://stackoverflow.com/questions/39066012/index-out-of-bound-exception-when-setting-value-to-jtable
    public void setValueAt(Object vlaue, int row, int column){
       if (column==6){
            this.vals.set(row, (Integer)vlaue);
        }
        this.fireTableCellUpdated(row, column);
        this.fireTableDataChanged();

    }

    /*public void setCols(String[] columns){
        cols = columns;
    }
    public void setClasses(Class[] classes){
        colClasses = classes;
    }*/
}
}

1 个答案:

答案 0 :(得分:2)

  

数据将被删除并返回null

您的getValuaAt(..)setValueAt()方法不同步。

setValueAt()中,您只保存第6列的数据。

getValueAt()中,您只返回第0-5列的数据,并为第6列返回null。

您需要修复getValueAt(...)方法以返回实际数据,而不是null。

此外,setValueAt(...)方法只应调用fireTableCellUpdated(...)方法而不是fireTableDataChanged(...)方法。