Adding Rows to a JTable
I am trying to add a row (or more) to a JTable on a button push, with the following method (explanation below):
The jTable and DefaultTableModel were created in NetBeans Swing pallette with some customisation to allow me to access toe DefaultModelTable:
The table starts out with no rows, although the column headers are visible. When the button is pushed, the code iterates through a LinkedList<Item> (Item is a superclass to Power) stored in the static Basket class, and takes data from each item to add it to the column.
I have confirmed that there are items are correctly stored in the basket.
The data is converted to Strings in one way or another and should be added to the table as an array using the tableModel1.addRow() method.
My trace statements all fire, so the code definitely reaches this line when the code is ran, but the row is not added to the table for some reason, because when I print out the row count it remains zero.
I am not getting any exceptions or error messages.
I am also aware that fireTableDataChanged() isn't necessary when using addRow(), it didn't help with or without.
Help would be greatly appreciated!
I wonder if you have more than one DefaultTableModel object, one that is used by the displayed JTable and that has no rows, and another distinct DefaultTableModel object that is not displayed but that gets rows added to it. We will need to see more code I think to solve this, and the best code to create and post here (if possible) is an SSCCE.
You must have excellent debugging intuition! I was actually using 3 JTables and 3 DefaultDataModels, unfortunately named jTable1, 2 and 3 and tableModel1, 2 and 3; I took two out to push my post towards SSCCE.
I was testing adding rows on jTable2 only, but had assigned it tableModel3. Spotted this after reading your suggestion. It is working now, thank you!
I've tripped over this problem myself -- we all have. Glad you've got it fixed.