【发布时间】:2018-10-10 16:24:43
【问题描述】:
我目前在将值设置为 JTable 中的复选框时遇到问题。 我需要将每个复选框的布尔值存储在表的行中。 我可以显示默认(假)复选框,但是一旦我单击复选框,就会出现异常; java.lang.ArrayIndexOutOfBoundsException:0。 我对带有布尔值的多数组不太熟悉,我似乎无法弄清楚我的代码的哪一部分是不正确的。
public static final int CHECKBOX= 0;
private final List<Data> datas;
private static boolean CHECKBOX_RENDERED[][] = new boolean[][]{};
private static Arrays array = null;
@Override
public Object getValueAt(int rowIndex, int columnIndex) {
if (rowIndex >= this.datas.size()) {
return null;
}
Data theData= this.datas.get(rowIndex);
if (theData== null) {
return null;
}
//Initialize the boolean field with table dimension
CHECKBOX_RENDERED = new boolean[datas.size()][columnIndex];
switch(columnIndex) {
case CHECKBOX:
array.fill(CHECKBOX_RENDERED[rowIndex], false);
return false; //default
default:
throw new IllegalArgumentException("Invalid column index");
}
}
@Override
public void setValueAt(Object value, int row, int column) {
if (column == CHECKBOX) {
Data data= this.datas.get(row);
if (data!= null && value instanceof Boolean) {
if (CHECKBOX_RENDERED[row][column]) {
CHECKBOX_RENDERED[row][column] = false;
} else {
CHECKBOX_RENDERED[row][column] = true;
}
fireTableCellUpdated(row, column);
}
}
}
【问题讨论】:
-
如需尽快获得更好的帮助,请发帖minimal reproducible example 或Short, Self Contained, Correct Example。
-
有了 camickr 的回答,我能够找到其他地方来更正我的代码并解决了这个问题。我移动了 CHECKBOX_RENDERED = new boolean[datas.size()][columnIndex];在加载数据的行之后。我完全删除了数组字段并返回 CHECKBOX_RENDERED[rowIndex][columnIndex];在switch语句的checkbox的情况下。
-
...after the line where data is loaded.- 这个代码甚至没有包含在问题中。这就是为什么您被要求在每个问题上都发布适当的“MCVE/SSCCE”。
标签: java swing multidimensional-array checkbox jtable