首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >java.lang.ArrayIndexOutOfBoundsException: 21 >= 21

java.lang.ArrayIndexOutOfBoundsException: 21 >= 21
EN

Stack Overflow用户
提问于 2015-09-29 10:40:49
回答 1查看 1.3K关注 0票数 0

我收到了下面的错误消息。

代码语言:javascript
复制
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)

在下面代码的第三行。

代码语言:javascript
复制
jTable.removeColumn(jTable.getColumnModel().getColumn(19));
jTable.removeColumn(jTable.getColumnModel().getColumn(20));
jTable.removeColumn(jTable.getColumnModel().getColumn(21));
jTable.removeColumn(jTable.getColumnModel().getColumn(22));

我已经在DefaultTableModel中添加了第21和第22列。

代码语言:javascript
复制
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];
            }

        });

但我不知道,为什么会出现ArrayIndexOutOfBounds异常。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2015-09-29 11:25:15

您可以调用JTable.removeColumn,列数组的每一列都将被重新编入索引。例如,当元素0被移除时,在索引1处的元素现在在索引0处被重新索引。

您需要以相反的顺序删除这些列,这样才不会重新编制索引:

代码语言:javascript
复制
jTable.removeColumn(jTable.getColumnModel().getColumn(22));
jTable.removeColumn(jTable.getColumnModel().getColumn(21));
jTable.removeColumn(jTable.getColumnModel().getColumn(20));
jTable.removeColumn(jTable.getColumnModel().getColumn(19));

您还可以调用4次以下行:

代码语言:javascript
复制
jTable.removeColumn(jTable.getColumnModel().getColumn(19));

因为在每次调用i时,19 + i列都将变成第19列。

票数 5
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/32841823

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档