将值设置为JTable时,索引超出范围异常 [英] Index Out of Bound Exception when setting value to JTable
问题描述
我在面板中有一个JTable
和表格模型.我在表中有一个复选框,该复选框应控制另一行是可编辑的,而其他行是食物对象的数据.但是,当我单击复选框时,在this.bools.set(row, (Boolean)vlaue) ;
行出现了java.lang.IndexOutOfBoundsException
异常.
I have a JTable
and table model in a panel. I have check box in the table which should control another row to be editable and the other rows are data of food object. But when I click on check box I get java.lang.IndexOutOfBoundsException
exception at this.bools.set(row, (Boolean)vlaue) ;
line.
为什么会出现此错误?
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;
}*/
}
}
推荐答案
原因是您试图在ArrayList中某个位置设置一个值,该值大于列表的大小.最初创建的ArrayList的大小为0.请阅读有关ArrayList的文档和教程.
The reason is that you are trying to set a value at a position in the ArrayList that is bigger then the size of the list. Initially ArrayList is created with size 0. Please read the documentation and tutorials about ArrayList.
代码中还有其他问题,例如当您尝试为布尔值赋值时:
There are also other problems in the code, e.g. when you try to assign a value to a boolean:
al = new ArrayList<Food>();
bools = new ArrayList<>(al.size());
for(Boolean b: bools){
b=Boolean.FALSE;
}
这基本上什么都不做.首先,列表在创建时为空,其次,赋值仅给b赋了一个新值,而不像您期望的那样更改bool中任何元素的内容.
This basically does nothing. First of all, the list is empty when created, second, the assignment only assigns a new value to b, but doesn't change the contents of any elements in bools, as you might expect it to do.
这篇关于将值设置为JTable时,索引超出范围异常的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!