1

I'm trying to design some java program that show content of MySQL database in JTable. I dont know what is better: to load whole table to a Vector, and then put data for each cell of table from it. Or load each cell as separate query?

public class DBTableMode extends AbstractTableModel {   
...        
Vector<Vector<Object>> data = dao.getWholeData(); //my getter of whole data table
...
public Object getValueAt(int rowIndex, int columnIndex) {
        data.elementAt(rowIndex).elementAt(columnIndex);
}
...

OR

public class DBTableMode extends AbstractTableModel {
...
public Object getValueAt(int rowIndex, int columnIndex) {
        dao.getCell(rowIndex, columnIndex);

}
...

Actually I don't know the operational principle of JTable, if it loads data onScrolling then second variant is much better. So, What is better in my case?

mKorbel
  • 109,525
  • 20
  • 134
  • 319
Yurgen
  • 123
  • 1
  • 13
  • How big is your table likely to be? – DNA Mar 13 '12 at 18:51
  • It has no practical application, this is experimental problem, so I'm interested what is better when table has few hundreds and when it contain few thousands of records. – Yurgen Mar 13 '12 at 19:05
  • 1
    _I don't know the operational principle of JTable_ - not bad in itself, simply an incentive to learn: http://docs.oracle.com/javase/tutorial/uiswing/components/table.html :-) – kleopatra Mar 14 '12 at 00:05

1 Answers1

2

It's hard to beat the convenience of DefaultTableModel, but AbstractTableModel offers more flexibility over the internal data representation. I'd use something more recent and without the synchronization overhead of Vector. This example uses Map<String, String>. In any case, by encapsulating the choice inside your TableModel, you preserve the option of changing your mind as the program evolves.

Also consider SwingWorker, shown here, to build the model in the background.

JTable itself is just the view. Its rendering is fairly efficient already.

Community
  • 1
  • 1
trashgod
  • 203,806
  • 29
  • 246
  • 1,045
  • 1
    +1 @Yurgen maybe best of ways is spliting Sql ResultSet to a few separated ResultSets ---> MySQL implementing paginations in (Select .... LIMIT int, int), – mKorbel Mar 13 '12 at 19:11