I got the below error message.
java.lang.ArrayIndexOutOfBoundsException: 21 >= 21
at java.util.Vector.elementAt(Vector.java:427)
at javax.swing.table.DefaultTableColumnModel.getColumn(DefaultTableColumnModel.java:277)
at com.vanuston.medeil.uitables.PurchaseTable.createTable(PurchaseTable.java:182)
at com.vanuston.medeil.ui.Purchase.applyDefaults$(Purchase.fx:130)
on the third line of the below code.
jTable.removeColumn(jTable.getColumnModel().getColumn(19));
jTable.removeColumn(jTable.getColumnModel().getColumn(20));
jTable.removeColumn(jTable.getColumnModel().getColumn(21));
jTable.removeColumn(jTable.getColumnModel().getColumn(22));
I already added the 21st and 22nd column to the DefaultTableModel
.
Vector cols = new Vector();
Vector data = new Vector();
int len = colName.length;
System.out.println("col length " + len);
for (int i = 0; i < initRow; i++) {
Vector c = new Vector();
for (int j = 0; j < len; j++) {
if (j == 19 && j == 20) {
c.addElement(Boolean.FALSE);
} else {
c.addElement(null);
}
}
data.addElement(c);
}
for (int n = 0; n < len; n++) {
cols.addElement(colName[n]);
System.out.println(colName[n]);
}
try {
jTable.setModel(new javax.swing.table.DefaultTableModel(data, cols) {
Class[] type = types;
boolean[] canEditCol = canEditCols;
@Override
public Class getColumnClass(int columnIndex) {
return type[columnIndex];
}
@Override
public boolean isCellEditable(int rowIndex, int columnIndex) {
return canEditCol[columnIndex];
}
});
But I don't know, what's the reason for showing ArrayIndexOutOfBounds
exception.
Well you call JTable.removeColumn
, each column of the array of columns is getting reindexed. For example, when element 0 is removed, the element that was at index 1 is now reindexed at index 0.
You need to remove those columns in reverse order, so that this reindexing doesn't happen:
jTable.removeColumn(jTable.getColumnModel().getColumn(22));
jTable.removeColumn(jTable.getColumnModel().getColumn(21));
jTable.removeColumn(jTable.getColumnModel().getColumn(20));
jTable.removeColumn(jTable.getColumnModel().getColumn(19));
You could also call 4 times the following line:
jTable.removeColumn(jTable.getColumnModel().getColumn(19));
since at each call i
, column 19 + i
will become column 19.