You're doing two things that are very dangerous:
- You're overriding
fireTableDataChanged()
and giving it an empty method body thereby bypassing all the magic that the super's method contains and making it essentially a worthless method, - and you're not calling any of the
fireXXXChanged()
methods after changing the model's data.
I recommend that you do the exact opposite:
- Don't override
fireTableDataChanged()
- Call the
fireXXX()
methods when you change data.
For example:
public void addElement(Person person) {
data.add(person);
int firstRow = data.size() - 1;
int lastRow = firstRow;
fireTableRowsInserted(firstRow, lastRow);
}
// public void fireTableDataChanged() {
//
// }
As an aside: why do you want to add a listener to the JTable or its TableModel?
Edit 2
You state:
I thought that i have to implement one of the firexxx method.
The reason that your class extends the AbstractTableModel class rather than implementing the TableModel interface is to get all the goodies that the AbstractTableModel class has to offer, including its fireXXX(...)
methods. They're already wired to notify any JTables that use the model that the data has changed. If you override them, and especially if you override them and don't call any of the super methods, you've broken this helpful code and it will no longer work.
As for LinkedList, i wanted to try use another collection(no array like in all samples).
Since you will usually want to access the data in a random way, I think you'd get more money out of an ArrayList. Regardless, the collection variable should probably be of List interface type so you can change the data type of the collection object as needed.
I don't understand why when i use your implementation it doesn't work.
If you say something "doesn't work" and you don't explain how or why it's not working, what errors you may be seeing, and without showing your code implementation, you tie our hands and prevent us from being able to help you effectively. If you need our help, consider giving us enough information to allow us to understand your problem. This will include showing us any changes to your code, showing any compilation errors or exception messages, and describing in detail the specifics of your problems.
Edit 3
In your most recent code iteration, your addElement method does everything but add an element!
public void addElement(Person person) {
int firstRow = data.size() - 1;
int lastRow = firstRow;
fireTableRowsInserted(firstRow, lastRow);
}
You don't add anything to the LinkedList in the method above!