带有文件 i/o 和数组列表的 JTable [英] JTable with file i/o and array list
问题描述
在我的程序中,用户输入单词及其相应的定义.这个用户定义的对象的一个例子是[面容,一个人的脸].用户的话存储在一个数组列表中,该列表与文件 i/o 一起使用.但是,每次我调用prepareTable"方法时,程序将文本文件中找到的单词的重复项添加到数组列表中.如果您需要查看更多代码,我可以发布它,但为了方便/可读性,我只发布了 prepareTable 方法.为什么我的程序会复制单词?这个方法有问题吗?
In my program users input words along with their corresponding definition. An example of this user defined object is [countenance, a person's face]. The user's words are stored in an array list which works with file i/o. However, each time I call the "prepareTable" method, the program adds duplicates of the words found in the text file to the array list. If you need to see more code, I can post it but for convenience/readability I only posted the prepareTable method. Why is my program duplicating the words? Is there something wrong with this method?
public void prepareTable ()
{
readFromFile();
for (int i = 0; i <= LibraryWordsList.size() - 1; i++)
{
tableData.setValueAt(LibraryWordsList.get(i).getWord(), i, 0);
tableData.setValueAt(LibraryWordsList.get(i).getDefinition(), i, 1);
}
}
推荐答案
如果 tableData
引用了 DefaultTableModel
的一个实例,你可以调用 setRowCount(0)
在添加新条目之前清除以前的条目.您可以使用表的 getModel()
方法获取对表的 TableModel
的引用.
If tableData
refers to an instance of DefaultTableModel
, you can invoke setRowCount(0)
to clear the previous entries before adding new ones. You can get a reference to the table's TableModel
by using the table's getModel()
method.
这篇关于带有文件 i/o 和数组列表的 JTable的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!