问题
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.
Why do I get this error?
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;
}*/
}
}
回答1:
Your bools ArrayList<Boolean>
is completely empty and can never be anything but empty in your code above (where do you ever fill it with anything other than empty ArrayLists?), so whenever you try to access it via, bools.set(row, (Boolean)vlaue)
you're going to get this exception. Solution: fill it before using it, and test that it's filled before calling a set method on the ArrayList. Use the ArrayList's add(...)
method if the size is 0 or if you're trying to set an item that doesn't exist.
You know of course that this:
for(Boolean b: bools){
b=Boolean.FALSE;
}
does nothing if bools.size() is 0 (which it is)
回答2:
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;
}
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.
来源:https://stackoverflow.com/questions/39066012/index-out-of-bound-exception-when-setting-value-to-jtable