-4
DefaultTableModel model2 = (DefaultTableModel) mytable.getModel();

What is the difference between these two?

model2.getValueAt(row,column);
mytable.getValueAt(row,column);
Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
  • You might notice this question has been down voted 3 times. My best guess is that people don't feel you have done enough research. Have you checked the JavaDocs for each classes version of the method? Have you worked through the tutorial on [How to Use Tables](https://docs.oracle.com/javase/tutorial/uiswing/components/table.html)? – Andrew Thompson Dec 24 '19 at 11:46
  • In most cases there is no difference. However if the table is filtered or sorted there is a difference. Or if the user reorders the columns there is a difference. – camickr Dec 24 '19 at 15:27

2 Answers2

1

From the JavaDoc - if you use JTable.getValueAt(int, int), you'll get the column in the table's visible column order. If a user reorders columns, the view and the model may not be in sync.

"Note: The column is specified in the table view's display order, and not in the TableModel's column order. This is an important distinction because as the user rearranges the columns in the table, the column at a given index in the view will change. Meanwhile the user's actions never affect the model's column ordering."

Riaan Nel
  • 2,425
  • 11
  • 18
-1

model.getValueAt(row, column)

for (int count = 0; count < model.getRowCount() ; count++)
{
  System.out.println( model.getValueAt(count, 0).toString());
}
Adir Dayan
  • 1,308
  • 13
  • 21