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?
It’s hard to beat the convenience of
DefaultTableModel, butAbstractTableModeloffers more flexibility over the internal data representation. I’d use something more recent and without the synchronization overhead ofVector. This example usesMap<String, String>. In any case, by encapsulating the choice inside yourTableModel, you preserve the option of changing your mind as the program evolves.Also consider
SwingWorker, shown here, to build the model in the background.JTableitself is just the view. Its rendering is fairly efficient already.