JTable中使用文件I / O和数组列表 [英] JTable with file i/o and array list

查看:113
本文介绍了JTable中使用文件I / O和数组列表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的程序的用户输入单词及其相应定义一起。
这个用户自定义对象的一个​​例子是[面容,一个人的脸。
用户的字存储在与文件I / O工作的一个数组列表。
然而,每次我所说的prepareTable的方法,程序
增加了在文本文件到数组列表中找到的话重复。如果您需要查看更多code,我可以张贴,但为了方便/可读性,我只贴prepareTable方法。为什么我的程序复制的话?有什么不对的方法?

 公共无效prepareTable()
{
    readFromFile();
    的for(int i = 0; I< = LibraryWordsList.size() - 1;我++)
    {
       tableData.setValueAt(LibraryWordsList.get(ⅰ).getWord()中,i,0);
       tableData.setValueAt(LibraryWordsList.get(ⅰ).getDefinition()中,i,1);
    }
}


解决方案

如果资料表的DefaultTableModel ,你可以调用添加新的前 setRowCount(0)来清除previous条目。你可以得到一个参考表的的TableModel 使用表的 getModel()方法。

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);  
    }
}

解决方案

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.

这篇关于JTable中使用文件I / O和数组列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆