问题描述
当我想从表中获取一个单元格值(经过过滤搜索后)并选择该行并执行returnAction()
时,就会发生异常.
When i want to get a cell value from my table (after search with filtering) and select that row and execute the returnAction()
, Exception occur.
我的代码:
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)
推荐答案
我假设(或者这实际上是原因),您实际上遇到了以下行的问题:
I am assuming(or this is actually the reason), You are actually having the issue with the following line:
String oldStatus = String.valueOf(table.getValueAt(rowInModel, 3));
JTable.getValueAt(row, col)
方法将通过将单元格(row, col)
的值转换为 model-index 来返回单元格(row, col)
的值,其中(row, col)
是 view-index .让我们来看看JTable
类的源代码:
JTable.getValueAt(row, col)
method will return the value of the cell (row, col)
by converting them to model-index where (row, col)
is view-index. Let use look into the source of JTable
class:
public Object getValueAt(int row, int column) {
return getModel().getValueAt(convertRowIndexToModel(row),
convertColumnIndexToModel(column));
}
因此,您传递给此函数的(row, col)
应该属于视图而不是模型.在您的上下文中为rowInTable
.从table.convertRowIndexToModel(rowInTable)
获得的rowInModel
已经是模型索引.通过调用JTable.getValueAt(rowInModel, 3)
,您实际上是在尝试将模型索引转换为另一个模型索引,从而导致计算错误.
So your passed (row, col)
to this function should belongs to the view, instead of model. In your context which is rowInTable
. The rowInModel
is already a model index as you got it from table.convertRowIndexToModel(rowInTable)
. By calling JTable.getValueAt(rowInModel, 3)
you are essentially trying to convert a model index to another model index resulting in computational error.
这篇关于设置行过滤器的表中的getValueAt时发生ArrayOutOfBoundException的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!