问题
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