JTable with file i/o and array list

微笑、不失礼 提交于 2019-12-20 07:14:50

问题


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

回答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.



来源:https://stackoverflow.com/questions/15443304/jtable-with-file-i-o-and-array-list

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!