ArrayOutOfBoundException来自设置行过滤器的表中的getValueAt

时间:2013-11-19 16:26:21

标签: java swing jtable indexoutofboundsexception rowfilter

当我想从我的表中获取一个单元格值(在使用过滤搜索之后)并选择该行并执行returnAction()时,会发生异常。

我的代码:

public class BookPage_User extends JFrame implements ActionListener {

private JButton returnBookBtn;
private JTextField filterTF;
private TableRowSorter sorter;
private JTable table;
private BookJDBC bookJDBC;
private BookModel model;

public BookPage_User(String[] enterUserInfo, String userId) {
    bookJDBC = new BookJDBC();
    model = new BookModel(bookJDBC.getData(), bookJDBC.getColumn());

    sorter = new TableRowSorter<TableModel>(model);
    table = new JTable(model);
    table.setRowSorter(sorter);

    add(createForm(), BorderLayout.NORTH);
    add(new JScrollPane(table), BorderLayout.CENTER);

    RowFilter<BookModel, Object> rf = null;
    rf = RowFilter.regexFilter(filterTF.getText(), 0);
    sorter.setRowFilter(rf);

    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    setSize(850, 600);
    setVisible(true);
}
public JPanel createForm() {
    JPanel panel = new JPanel(new FlowLayout(FlowLayout.LEFT));
    returnBookBtn = new JButton("Return Book");
    filterTF = new JTextField(10);
    filterTF.getDocument().addDocumentListener(new DocumentListener() {
        @Override
        public void insertUpdate(DocumentEvent e) {
            String key = filterTF.getText().trim();
            if (key.length() == 0) sorter.setRowFilter(null);
            else sorter.setRowFilter(RowFilter.regexFilter(key, 1));
        }

        @Override
        public void removeUpdate(DocumentEvent e) {
            String key = filterTF.getText().trim();
            if (key.length() == 0) sorter.setRowFilter(null);
            else sorter.setRowFilter(RowFilter.regexFilter(key, 1));
        }

        @Override
        public void changedUpdate(DocumentEvent e) {
            //To change body of implemented methods use File | Settings | File Templates.
        }
    });

    returnBookBtn.addActionListener(this);
    panel.add(returnBookBtn);
    panel.add(filterTF);
    return panel;
}
@Override
public void actionPerformed(ActionEvent e) {
    if (e.getSource() == returnBookBtn) returnAction();
}

public void returnAction() {
    if (table.getSelectedRow() > -1) {
        int rowInTable = table.getSelectedRow();
        int rowInModel = table.convertRowIndexToModel(rowInTable);
        String oldStatus = String.valueOf(table.getValueAt(rowInModel, 3));  // Exception occur here!
        String returnBookId = String.valueOf(table.getValueAt(rowInModel, 0));
        if (oldStatus.equalsIgnoreCase("Yes")) {
                model.updateBooksTableReturnAction(rowInModel);
                model.deleteFromBorrowTable(returnBookId);
        } else {
            JOptionPane.showMessageDialog(null, "This Book Not Borrowed");
            return;
        }
    } else JOptionPane.showMessageDialog(null, "Select a book");
}

我得到的例外情况如下 -

Exception in thread "AWT-EventQueue-0" java.lang.ArrayIndexOutOfBoundsException: 8
at javax.swing.DefaultRowSorter.convertRowIndexToModel(DefaultRowSorter.java:518)
at javax.swing.JTable.convertRowIndexToModel(JTable.java:2645)
at javax.swing.JTable.getValueAt(JTable.java:2720)
at Project.BookPage_User.returnAction(BookPage_User.java:125)
at Project.BookPage_User.actionPerformed(BookPage_User.java:95)
at javax.swing.AbstractButton.fireActionPerformed(AbstractButton.java:2018)
at javax.swing.AbstractButton$Handler.actionPerformed(AbstractButton.java:2341)
at javax.swing.DefaultButtonModel.fireActionPerformed(DefaultButtonModel.java:402)
at javax.swing.DefaultButtonModel.setPressed(DefaultButtonModel.java:259)
at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(BasicButtonListener.java:252)
at java.awt.Component.processMouseEvent(Component.java:6505)
at javax.swing.JComponent.processMouseEvent(JComponent.java:3321)
at java.awt.Component.processEvent(Component.java:6270)
at java.awt.Container.processEvent(Container.java:2229)
at java.awt.Component.dispatchEventImpl(Component.java:4861)
at java.awt.Container.dispatchEventImpl(Container.java:2287)
at java.awt.Component.dispatchEvent(Component.java:4687)
at java.awt.LightweightDispatcher.retargetMouseEvent(Container.java:4832)
at java.awt.LightweightDispatcher.processMouseEvent(Container.java:4492)
at java.awt.LightweightDispatcher.dispatchEvent(Container.java:4422)
at java.awt.Container.dispatchEventImpl(Container.java:2273)
at java.awt.Window.dispatchEventImpl(Window.java:2719)
at java.awt.Component.dispatchEvent(Component.java:4687)
at java.awt.EventQueue.dispatchEventImpl(EventQueue.java:723)
at java.awt.EventQueue.access$200(EventQueue.java:103)
at java.awt.EventQueue$3.run(EventQueue.java:682)
at java.awt.EventQueue$3.run(EventQueue.java:680)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:76)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:87)
at java.awt.EventQueue$4.run(EventQueue.java:696)
at java.awt.EventQueue$4.run(EventQueue.java:694)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:76)
at java.awt.EventQueue.dispatchEvent(EventQueue.java:693)
at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:242)
at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:161)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:150)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:146)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:138)
at java.awt.EventDispatchThread.run(EventDispatchThread.java:91)

3 个答案:

答案 0 :(得分:2)

ArrayIndexOutOfBounds只是说您正在尝试访问超出表示表的行和列的内部数组边界的行或列。

确保为模型和表使用正确的索引。请记住,表格渲染模型的方式可能略有不同,因为用户可能会订购行,交换列等。

如果值在表格单元格中,并且您知道它的行和列,并且您想从那里获取它,请使用rowInTable而不是rowInModelrowInModel可能不同,因此要求表使用此索引为您提供行的值可能会给您错误的值,如果要过滤掉行,您可能会遇到这些ArrayIndexOutOfBounds异常。

完整详情here

答案 1 :(得分:1)

我假设(或者这实际上是原因),您实际上遇到了以下问题:

String oldStatus = String.valueOf(table.getValueAt(rowInModel, 3));

JTable.getValueAt(row, col)方法将返回单元格(row, col)的值,方法是将它们转换为 model-index ,其中(row, col) view-index 。让我们看一下JTable类的来源:

public Object getValueAt(int row, int column) {
        return getModel().getValueAt(convertRowIndexToModel(row),
                                     convertColumnIndexToModel(column));
    }

所以你传递给(row, col)这个函数应该属于视图而不是模型。在您的上下文中rowInTable。从rowInModel获取table.convertRowIndexToModel(rowInTable)已经是模型索引了。通过调用JTable.getValueAt(rowInModel, 3),您实际上是在尝试将模型索引转换为另一个模型索引,从而导致计算错误

答案 2 :(得分:0)

过滤表时,显示值的列/行编号与原始表模型的基础列/行编号不同。因此,您需要相应地转换行号或列号以避免arrrayOutOfBounds异常。

假设您的表名为jTableItems,以下简单的if ... else ..条件将确保正确返回的行和col值指向所选数据,无论表是否已排序:

 if (jTableItems.getSelectedColumn() == jTableItems.convertColumnIndexToView(jTableItems.getSelectedColumn())) {
            col = jTableItems.getSelectedColumn();
            row = jTableItems.getSelectedRow();
        } else {
            col = jTableItems.convertColumnIndexToView(jTableItems.getSelectedColumn());
            row = jTableItems.convertRowIndexToView(jTableItems.getSelectedRow());

        }

祝你好运!#/ p>