- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我是 Java 的新手。我现在才学半年。现在我正在为学校做一个项目,但我遇到了障碍:
我基本上正在制作一个程序来管理您自己的书籍。我有一个类“Books”,它保存书籍对象的数据。然后是“Library”类,它包含书籍的ArrayList。对于 TableModel,我使用扩展 AbstractTableModel 的类(“LibraryTableModel”)。然后我有一个 GUI 类来显示该表。
该表实际上可以工作,但有两个实例导致程序崩溃:
当我将一本书添加到空图书馆时,表格不会更新。但是,当我重新启动程序时,这本书被添加了(我将 Library 类保存为 .ser 文件)。
然后是我要询问的实例:我有一个按钮可以从表中删除书籍。按钮本身工作正常,但是当我删除一本书时,程序会抛出 ArrayOutOfBoundsException。当我重新创建表时,它会更新并且书籍会被删除。这里有什么问题,为什么程序崩溃而不是更新表?
TableModel 代码:
public class LibraryTableModel extends AbstractTableModel
{
private String[] columnNames = {"Titel", "Autor", "Status", "Genre", "Verlag", "Seitenzahl", "ISBN", "Sprache", "Bewertung"};
private Object[][] data = {};
ArrayList<Book> lib;
public LibraryTableModel(Library l)
{
//This Method returns the ArrayList in the Library class
lib = l.getList();
int libSize = lib.size();
data = new Object[bib.size()][];
for (Book b : lib)
{
int index = bib.indexOf(b);
//(...)
//CODE HERE gets all the data that is supposed to be displayed
//from each book in the ArrayList
Object[] oA = {tit, aut, sta, gen, ver, sei, isb, spr, bew};
data[index] = oA;
}
}
public int getColumnCount()
{
return columnNames.length;
}
public int getRowCount()
{
return data.length;
}
public String getColumnName(int col)
{
return columnNames[col];
}
public Object getValueAt(int row, int col)
{
return data[row][col];
//When I try to remove a Book, the ArrayOutOfBounds Exception comes from here
}
public Class getColumnClass(int c)
{
return getValueAt(0, c).getClass();
}
public void setValueAt(Object value, int row, int col)
{
data[row][col] = value;
fireTableCellUpdated(row, col);
}
public void removeRow(int row)
{
lib.remove(row);
fireTableRowsDeleted(row, row);
}
GUI类中围绕表和表模型的代码:
public class GUI implements ActionListener
{
JTable table;
LibraryTableModel model;
TableRowSorter<BibliothekTableModel> sorter;
Library lib;
JMenuItem deleteBook;
(...) 库通过 .ser 文件加载
public void showTable() //This method is envoked in the GUI constructor through pressing a button
{
model = new LibraryTableModel(lib);
table.setModel(model);
deleteBook.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
int row = table.getSelectedRow();
model.removeRow(row);
//Code that saves the library at this point
table.setModel(new LibraryTableModel(lib));
}
});
popupMenu.add(deleteBook);
table.setComponentPopupMenu(popupMenu);
sorter = new TableRowSorter<BibliothekTableModel>(model);
table.setRowSorter(sorter);
JScrollPane scrollTable = new JScrollPane(table);
//Next is code, that adds this ScrollPane to my Frame
}
最佳答案
When I add a Book to an empty Library, the table doesn't update. However, the Book IS added when I start the program anew (I save the Library class as a .ser file).
没有提供任何信息来演示其工作原理...什么是 .ser
文件?
Then the instance which I am asking about: I have a button that removes Books from the table. the Button itself works fine, but when I remove a book, the program throws an ArrayOutOfBoundsException. When I create the table anew, it updates and the book is removed. What is the problem here, why does the program crash instead of update the table?
有两个问题...
首先,因为您在表上使用 RowSorter
,所以 JTable#getSelectedRow
返回的可视行索引和模型中的物理行索引将不会被同样,您需要使用JTable#convertRowIndexToModel
int row = table.getSelectedRow();
row = table.convertRowIndexToModel(row);
model.removeRow(row);
//Code that saves the library at this point
其次,您将从 lib
中删除该书,但不更新内部缓存...
public void removeRow(int row)
{
lib.remove(row);
fireTableRowsDeleted(row, row);
}
模型未使用 lib
作为数据源,而是使用 data
数组,而您尚未更新该数组。
虽然您可以简单地重建 data
数组,但更好的解决方案是摆脱它并直接使用 lib
,例如...
public class LibraryTableModel extends AbstractTableModel {
private String[] columnNames = {"Titel", "Autor", "Status", "Genre", "Verlag", "Seitenzahl", "ISBN", "Sprache", "Bewertung"};
private Object[][] data = {};
private Library lib;
public LibraryTableModel(Library l) {
lib = l;
}
public int getColumnCount() {
return columnNames.length;
}
public int getRowCount() {
return lib.getList().size();
}
public String getColumnName(int col) {
return columnNames[col];
}
public Object getValueAt(int row, int col) {
Book book = lib.getList().get(row);
Object value = null;
switch (col) {
case 0:
value = ...;
break;
case 1:
value = ...;
break;
case 2:
value = ...;
break;
case 3:
value = ...;
break;
case 4:
value = ...;
break;
case 5:
value = ...;
break;
case 6:
value = ...;
break;
case 7:
value = ...;
break;
case 8:
value = ...;
break;
}
return value;
//When I try to remove a Book, the ArrayOutOfBounds Exception comes from here
}
public Class getColumnClass(int c) {
// Don't do this, know the actualy value and return it
// Otherwise you could end up with a NullPointerException
return getValueAt(0, c).getClass();
}
public void setValueAt(Object value, int row, int col) {
// Use a simular technquie to getValueAt to extract the Book for the give
// row and update the Book's attributes
fireTableCellUpdated(row, col);
}
public void removeRow(int row) {
lib.remove(row);
fireTableRowsDeleted(row, row);
}
}
关于java - 从 JTable 中删除对象时出现 ArrayOutOfBoundsException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27893055/
目前我正在尝试用 java 打印一些 pdf。使用的 API 是 Apache PDF Box 2.0 版。转换图像后,我将它们写入磁盘以节省内存。在下一步中,我再次阅读它们并将标题写在图像的头部。之
打开应用程序时,有时会发现与Moshi相关的此异常: Caused by java.lang.ArrayIndexOutOfBoundsException: length=33; index=33
我正在尝试编写一个程序,当您输入数字时,该程序会输出数字的名称。它对于一位数字来说效果很好,但我遇到了障碍。每当我输入大于 10 的数字时,它都会给出 ArrayOutOfBoundsExceptio
import java.util.*; import java.util.ArrayList; class MyHashTable, E> { private ArrayList> bucke
我正在用 java 编写一个分析股票数据的程序。 我几乎让它工作了,但现在它给了我一个 ArrayOutOfBounds 异常。 int n = closingPrices.size();
假设 url = "http://gmail.com"。 我尝试从中创建一个字符串 dnsname = "Top-level com". 现在我们运行以下代码: System.out.println(
在 Python 中,如果您使用越界键/索引对集合结构进行索引,您会得到一记耳光: >>> [1, 2, 3][9] Traceback (most recent call last): File
我创建了一个在 Java 中执行值数组的方法。每当我尝试运行它时,都会遇到 ArrayOutOfBoundsException。我的控制台将错误指向是“if”语句。 for (int i = 1; i
我正在做一个关于基本 2D 塔防游戏的教程,我们基本上是试图用一堆图 block 填充一个窗口,但我在这里遇到了 ArrayOurOfBoundsException,我不明白为什么! map
我比较了 Java 中的 BufferedImage 对象。我从一组图像中获取图像,并将其与同一组中的所有图像进行比较。然而,它会导致这个奇怪的错误,将图像与其自身进行比较,并且对于其他图像来说一切正
我在这里不知所措,任何人都可以看出这段代码有什么问题吗?我在这个驱动程序中使用 sqlite: https://bitbucket.org/xerial/sqlite-jdbc/downloads 编
我收到一个 arrayoutofboundsexception 错误。当我尝试格式化要显示的草稿文本消息时。这只是一个简单的短信应用程序,我正在尝试设置文本大小和颜色。当我评论这两行时,我的应用程序没
public class WeightOnPlanetsV1 { public static double[] calcWeight(double[] gravity, double[]mass)
我在使用 replaceFirst 时得到一个奇怪的 ArrayOutOfBoundsException: "this is an example string".replaceFirst("(^th
已关闭。这个问题是 not reproducible or was caused by typos 。目前不接受答案。 这个问题是由拼写错误或无法再重现的问题引起的。虽然类似的问题可能是 on-top
我是 Java 的新手。我现在才学半年。现在我正在为学校做一个项目,但我遇到了障碍: 我基本上正在制作一个程序来管理您自己的书籍。我有一个类“Books”,它保存书籍对象的数据。然后是“Library
我尝试对以下格式的数据使用过滤后的分类器: 实数,实数,实数,...,标称 其中我有 138 个实值和一个代表该类的名义字符串。我使用 J48 作为基分类器和监督离散化过滤器,如下所示: Filter
我使用的是正确显示的 JList。但是,我无法从列表中删除元素。 JList nameList = new JList(db.getAllNames()); nameList.setVisibleRo
我今天了解了 java 中的 arraycopy() 函数,并且在代码中使用它。但我不断收到 ArrayOutOfBoundsException。我试图找出一个解决方案并在谷歌上搜索解决方案,但我似乎
我们不时看到 Android 的 ART 代码崩溃。我们在没有边界检查的情况下索引到一个 int 数组,然后捕获 Java 的 ArrayIndexOutOfBoundsException 并正确处理
我是一名优秀的程序员,十分优秀!