【问题标题】:Index Out of Bound Exception when setting value to JTable将值设置为 JTable 时出现索引越界异常
【发布时间】:2016-12-28 04:57:49
【问题描述】:

我在面板中有一个JTable 和表格模型。我在表格中有复选框,它应该控制另一行可编辑,其他行是食物对象的数据。但是当我单击复选框时,我在this.bools.set(row, (Boolean)vlaue) ; 行得到java.lang.IndexOutOfBoundsException 异常。

为什么会出现这个错误?

import com.sun.org.apache.xpath.internal.operations.Bool;
import Food;
import FoodDAO;

import javax.swing.*;
import javax.swing.table.AbstractTableModel;
import javax.swing.table.TableCellRenderer;
import java.awt.*;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Vector;

/**
* Created by debian on 8/20/16.
 */
public class FoodListPanel extends JPanel{
JTable jTable;
FoodTableModel fm;

public FoodListPanel() {

    try {
         fm = new FoodTableModel(FoodDAO.getAllFoodsFromDB(), new ArrayList<Boolean>(), 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, Boolean.class, Integer.class};
    ArrayList<Food> al;
    ArrayList<Boolean> bools;
    ArrayList<Integer> vals;
    public FoodTableModel(ArrayList<Food> foods, ArrayList<Boolean> boxes,ArrayList<Integer> val) {
        al = new ArrayList<Food>();
        bools = new ArrayList<>(al.size());
        for(Boolean b: bools){
            b=Boolean.FALSE;
        }
        vals = new ArrayList<Integer>(al.size());
        al.addAll(foods);
        bools.addAll(boxes);

    }


    ////// 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();
            //// TODO: 8/20/16  CASE 4  + PICTURE

            default:
                return null;
        }
    }


    public void setValueAt(Object vlaue, int row, int column){
        if(column==5){
            this.bools.set(row, (Boolean)vlaue) ;
        }else if (column==6){
            this.vals.set(row, (Integer)vlaue);
        }
    }

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

【问题讨论】:

标签: java swing jtable indexoutofboundsexception


【解决方案1】:

您的布尔值ArrayList&lt;Boolean&gt; 完全是空的,在上面的代码中只能是空的(除了空的 ArrayLists 之外,您在哪里填充它?),所以每当您尝试通过 bools.set(row, (Boolean)vlaue) 访问它时你会得到这个例外。解决方法:使用前先填充,并在调用ArrayList的set方法前测试是否填充。如果大小为 0 或尝试设置不存在的项目,请使用 ArrayList 的 add(...) 方法。

你当然知道:

    for(Boolean b: bools){
        b=Boolean.FALSE;
    }

如果 bools.size() 为 0(它是)则不执行任何操作

【讨论】:

    【解决方案2】:

    原因是您试图在 ArrayList 中大于列表大小的位置设置一个值。最初创建的 ArrayList 大小为 0。请阅读有关 ArrayList 的文档和教程。

    代码中还有其他问题,例如当您尝试将值分配给布尔值时:

        al = new ArrayList<Food>();
        bools = new ArrayList<>(al.size());
        for(Boolean b: bools){
            b=Boolean.FALSE;
        }
    

    这基本上什么都不做。首先,列表在创建时是空的,其次,赋值只为 b 赋值,但不会改变 bools 中任何元素的内容,正如你所期望的那样。

    【讨论】:

      猜你喜欢
      • 2018-01-05
      • 1970-01-01
      • 1970-01-01
      • 2018-11-19
      • 2013-03-30
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-04-17
      相关资源
      最近更新 更多