Duplicate Values in JTable Rows Resolved
Despite populating the JTable with a custom DataModel, repetitive data persisted in each row. However, upon closer inspection, the data model remained intact.
Investigation and Resolution
The issue stemmed from unintentionally referencing the same row data multiple times. The solution involved ensuring that each row contained a distinct array list.
Sample Code
Here's a complete code sample demonstrating the corrected implementation:
public void populate(Collection c) { data.clear(); for (Item i : c.getItems()) { ArrayList<String> row = new ArrayList<>(); // Create a new array list for each row for (Property p : i.getProperties().values()) { row.add(p.toString()); } data.add(row); } fireTableDataChanged(); }
By creating a separate array list for each row, the data model and JTable accurately displayed the intended data.
The above is the detailed content of How to Prevent Duplicate Rows in a JTable When Using a Custom Data Model?. For more information, please follow other related articles on the PHP Chinese website!