improve speed of JTable rows insertion
Hi guys,
I'm dynamically filling a JTable with data that is between 1000 and 2000.
i extended a DefaultTableModel in which i have a method that calls addRow() for each file added to JTable.
it seems that the addRow() method is slowing the process because it asks the table to repaint each time it is called.
I thought to completely eliminate the updating the table as a batch
I need help to write my own table model which internally keeps all the data as an array of File objects
i want to place all the data i need to add to table as a list of hash maps and write a table model that directly uses that list as the data set.
can someone show me some code for that ?
here is my current Table Model : (i suppose i need to change the addFile(File) method to make it addFile( File[]) and tell table to update after all File added not each time we call addRow())
publicclass FilePanelDataModelextends DefaultTableModel{
protected String[] columnNames =new String[]{"Name","Size","Date",
"Directory"};
protectedint[] columnSize =newint[]{ 230, 80, 100, 0};
protected Class[] columnClasses =new Class[]{ String.class, String.class,
String.class, String.class};
private SimpleDateFormat df =new SimpleDateFormat(
"dd/MM/yyyy, HH:mm:ss a");
FileSystemView fsv = FileSystemView.getFileSystemView();
publicvoid addFile(File file){
Object[] rowData =new Object[4];
if (!fsv.isHiddenFile(file)){
rowData[0] = fsv.getSystemDisplayName(file);
if (file.isDirectory())
rowData[1] =new String(" ");
elseif (file.isFile())
rowData[1] =new String(util.UploadRegistry.fileSize(file
.length()));
rowData[3] = file.getPath();
rowData[2] =new String(df.format(new Date(file.lastModified())));
}elseif (file.isDirectory() || file.isFile()){
rowData[0] = file.getName();
if (file.isDirectory())
rowData[1] =new String(" ");
elseif (file.isFile())
rowData[1] =new String(util.UploadRegistry.fileSize(file
.length()));
rowData[3] = file.getParent();
rowData[2] =new String(df.format(new Date(file.lastModified())));
}
addRow(rowData);
}
publicboolean contains(File file){
boolean contain =false;
for (int i = 0; i < getRowCount(); i++){
if (file.isDirectory() || file.isFile()){
if (file.getName().equalsIgnoreCase(getFileAt(i).getName())){// &&
// file.getParent().equalsIgnoreCase(
contain =true;
break;
}
}elseif (!fsv.isHiddenFile(file)){
if (fsv.getSystemDisplayName(file).equalsIgnoreCase(
getFileAt(i).getName())){// &&
// file.getParent().equalsIgnoreCase(
contain =true;
break;
}
}
}
return contain;
}
public File getFileAt(int row){
File file =new File((String) getValueAt(row, 3), (String) getValueAt(
row, 0));
if (!file.exists())
returnnew File((String) getValueAt(row, 3));
returnnew File((String) getValueAt(row, 3),
(String) getValueAt(row, 0));
}
publicint getColumnCount(){
return columnNames.length;
}
public String getColumnName(int col){
return columnNames[col];
}
publicint getColumnSize(int col){
return columnSize[col];
}
public Class getColumnClass(int col){
return columnClasses[col];
}
publicboolean isCellEditable(int row,int column){
returnfalse;
}
}
thanks for helping

