I created my data model class, inheriting AbstractTableModel . It stores a link to an ArrayList with entities. Here is a primitive example:

 package jtable_example; import java.util.ArrayList; import javax.swing.table.AbstractTableModel; public class MyTableModel extends AbstractTableModel{ ArrayList<Human> humans; MyTableModel(ArrayList<Human> humans) { super(); this.humans = humans; } @Override public int getRowCount() { return humans.size(); } @Override public int getColumnCount() { return 3; } @Override public Object getValueAt(int r, int c) { switch (c) { case 0: return humans.get(c).getName(); case 1: return humans.get(c).getSurname(); case 2: return humans.get(c).getTelephone(); default: return ""; } } @Override public String getColumnName(int c) { switch (c) { case 0: return "Name"; case 1: return "Surname"; case 2: return "Telephone"; default: return "Unknow"; } } } 

If I add a value to an ArrayList , it does not change in the table. How do I update a JTable when updating an ArrayList ?

    1 answer 1

    Found an option. After adding data to the ArrayList , you need to call the updateUI() method on the object of the JTable class.

    By the way, you can still perform the update by calling the fireDataChanged () method on the table model. I shoved him in the listener and now everything is okay.

    • If you look from the height of high matter, then this is the wrong decision. In theory, the table model has listeners .. they need to be notified about changes in the table and she will decide what to redraw and whether it should be. - cy6erGn0m
    • updateUI () - used to update the table, for example, after changing the sorting - Gorets
    • sorting is one thing, and updating the data itself is another. The table may not redraw anything at all if the data changes in cells that are not visible or redraw only the cells you need instead of drawing everything that is visible. - cy6erGn0m
    • I mean, if new records appeared in the database, updateUI () will not add them to the table =) - Gorets
    • Well, they are pulled from the database into the array. About which "knows" the table model. How to tell the table itself that if the array changes, the UI will be updated? - Jakeroid