gpt4 book ai didi

java - 从设置行过滤器的表中 getValueAt 时出现 ArrayOutOfBoundException

转载 作者:行者123 更新时间:2023-12-01 23:25:27 25 4
gpt4 key购买 nike

当我想从表中获取单元格值(在使用过滤进行搜索后)并选择该行并执行 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)如下 -

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)

最佳答案

ArrayIndexOutOfBounds只是说您正在尝试访问表示表的行和列的内部数组范围之外的行或列。

确保为模型和表使用正确的索引。请记住,表呈现模型的方式可能会有所不同,因为用户可能会对行进行排序、交换列等。

如果该值位于表格单元格中,并且您知道它的行和列,并且您想从那里获取它,请使用 rowInTable不是rowInModelrowInModel可能会有所不同,因此要求表使用此索引为您提供行的值可能会给您错误的值,并且如果行被过滤掉,您最终可能会得到这些 ArrayIndexOutOfBounds异常(exception)。

完整详细信息here .

关于java - 从设置行过滤器的表中 getValueAt 时出现 ArrayOutOfBoundException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20076777/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com