Read data from a JTable created from AbstractTableModel

If you have a JTable in Java created with an AbstractTableModel, you can read the data from it, using following:

int rowCount = testTable.getRowCount();
for (int i = 0; i < rowCount; i++) {
   String mColumn1 = (String) testTable.getModel().getValueAt(i, 0);
   String mColumn2 = (String) testTable.getModel().getValueAt(i, 1);
}

Alternatively, you could also try where the JTable is within a Panel:

int rowCount = testTable.getRowCount();
for (int i = 0; i < rowCount; i++) {
   JTable myTable = myPanel.testTable;           //<-----

   String mColumn1 = (String) myTable.getModel().getValueAt(i, 0);
   String mColumn2 = (String) myTable.getModel().getValueAt(i, 1);
}

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s