jtable question

hey all

i just wanna know...what is the most efficient way to add values from a database to a jtable....lets say i have a table in a database that have around 15000 tuple and i need to add those to a table in a jpanel....is a for loop with a body calling the method setvalueAt(Object data, int row, int column) a good and efficient way to do things ?

help please...

Thanks in advance

[408 byte] By [bif_fa] at [2007-11-27 4:14:55]
# 1
I would create your TableModel and load the data into the model and then use the table.setModel(...) methodl to update the table. This way the table only repaints itself once. If you use setValueAt(...). It will attempt to repaint every cell as its updated.
camickra at 2007-7-12 9:21:15 > top of Java-index,Desktop,Core GUI APIs...
# 2

class CustomerListGUITableModel extends AbstractTableModel {

int row = 1;

int column = 0;

private String[] columnNames = {"Name",

"Phone",

"Address",

"Note"};

private Object[][] data = {

{" ",

" ", " ", " "}

};

public int getColumnCount() {

return columnNames.length;

}

public int getRowCount() {

return data.length;

}

public String getColumnName(int col) {

return columnNames[col];

}

public Object getValueAt(int row, int col) {

return data[row][col];

}

/*

* JTable uses this method to determine the default renderer/

* editor for each cell. If we didn't implement this method,

* then the last column would contain text ("true"/"false"),

* rather than a check box.

*/

public Class getColumnClass(int c) {

return getValueAt(0, c).getClass();

}

/*

* Don't need to implement this method unless your table's

* data can change.

*/

public void setValueAt(Object value, int row, int col) {

data[row][col] = value;

}

public int getRow() {

return row;

}

public int getColumn() {

return column;

}

public void incRow() {

if (row == getRowCount()) {

System.err.println("Reached Bounds...");

} else

row++;

}

public void incColum() {

if(column == getColumnCount()){

System.err.println("Reached Bounds...");

}else

column++;

}

public void decRow(){

if(row == 1){

System.err.println("Reached Bounds...");

}else

row--;

}

public void decColumn(){

if(column == 0){

System.err.println("Reached Bounds...");

}else

column--;

}

public void setRow(int value) {

if(value<1){

System.err.println("row value must be greater or equal to 1");

return;

}

if(value > getRowCount()){

System.err.println("value entered must be within bounds");

return;

}

row = value;

}

public void setColumn(int value) {

if(column < 0){

System.err.println("column value must be greater or equal to 0");

return;

}

if(value > getColumnCount()){

System.err.println("value entered must be within bounds");

return;

}

column = value;

}

}

this is my table model....should i update the data object as i get values from the database ?

and after updating the data object i write something like table.setModel(new CustomerListGUITableModel()); ?

please help

bif_fa at 2007-7-12 9:21:15 > top of Java-index,Desktop,Core GUI APIs...
# 3

Why do you have a custom TableModel? The DefaultTableModel will work fine.

You create a Vector of Vectors with the data from the ResultSet from your database query. Then you create the DefaultTableModel using the Vector. Then you set the model of the table.

Simple example:

http://forum.java.sun.com/thread.jspa?forumID=57&threadID=5123381

camickra at 2007-7-12 9:21:15 > top of Java-index,Desktop,Core GUI APIs...
# 4

thanks a lot camickr... that was very helpful...but i still have some questions...

this code get the data and the column names at run time... my code does something else which is that it gets only the data when an actionPerformed is fired...so the table is always sitting there static having some column names, and only when the button is pressed the table is updated...so my question is should i create a dummy table and when the button is pressed, the table should be removed and another table inserted having approximatively the same code example you provided ?

please help

thanks in advance

bif_fa at 2007-7-12 9:21:15 > top of Java-index,Desktop,Core GUI APIs...