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

[7463 byte] By [Servant@AKa] at [2007-11-26 16:59:04]
# 1

I don't know about your code or JTable for that matter, however, the SWT GUI package has a Table class and you can tell it to *not* repaint itself, which is useful when you have to add tons of rows.

I don't know if you JTable has a function like that, but couldn't you also just try making it not visible or hide it somehow while you load it and then paint/show it once you are finished filling it?

den2681a at 2007-7-8 23:26:52 > top of Java-index,Java Essentials,Java Programming...
# 2
why call addRow so many times? can't you just update the model wholesale and then repaint the table? again, that's what I do in SWT, don't really deal with swing too much
georgemca at 2007-7-8 23:26:52 > top of Java-index,Java Essentials,Java Programming...
# 3

Lah-di-dah, look at all those SWT snobs :-)

JTable doesn't have any methods to turn off repainting (Component has setIgnoreRepaint, but I think it only applies to OS-level paint messages.)

I aggree with the last post: create a new model (either passing it all the data or filling it up after creation), and when the model is complete, *then* call setModel:

Vector data = new Vector();

//fill data with row vectors

TableModel model = new DefaultTableModel(data, columnNames);

table.setData(data);

Design concerns: Speaking just for myself, I don't want to stare at a table with 2,000 rows! How about adding search constraints or paging?

DrLaszloJamfa at 2007-7-8 23:26:52 > top of Java-index,Java Essentials,Java Programming...
# 4
> JTable doesn't have any methods to turn off repaintingNo, but adding to the model, you don't have too call the "fireNewRow" type method for each row. (Which IMO is better than turning of drawing, update, then turning drawing back on).
mlka at 2007-7-8 23:26:52 > top of Java-index,Java Essentials,Java Programming...
# 5
If you have created your own table model, what is to stop you overridding the addRow() method so it only issues instructions to re-paint the JTable after a number of new rows have been created?
Tillermana at 2007-7-8 23:26:52 > top of Java-index,Java Essentials,Java Programming...
# 6

> No, but adding to the model, you don't have too call

> the "fireNewRow" type method for each row. (Which IMO

> is better than turning of drawing, update, then

> turning drawing back on).

Note that with DefaultTableModel, methods like addRow automatically call a fire* method for you, whether you want them or not. You can implement you own TableModel (I would extend AbstractTableModel), if you like that kind of thing.

DrLaszloJamfa at 2007-7-8 23:26:52 > top of Java-index,Java Essentials,Java Programming...
# 7

> Lah-di-dah, look at all those SWT snobs :-)

>

> JTable doesn't have any methods to turn off

> repainting (Component has setIgnoreRepaint, but I

> think it only applies to OS-level paint messages.)

>

> I aggree with the last post: create a new model

> (either passing it all the data or filling it up

> after creation), and when the model is complete,

> *then* call setModel:

> > Vector data = new Vector();

> //fill data with row vectors

> TableModel model = new DefaultTableModel(data,

> columnNames);

> table.setData(data);

>

>

thank you : i found your reply the most interesting . i will try this method which i suppose will solve my problem.

> Design concerns: Speaking just for myself, I don't

> want to stare at a table with 2,000 rows! How about

> adding search constraints or paging?

can you explain me further this design proposition : it seems also interesting feature to add to my application.

Thanks also to all repliers to my post for their interesting Ideas.

Servant@AKa at 2007-7-8 23:26:52 > top of Java-index,Java Essentials,Java Programming...
# 8

> can you explain me further this design proposition : it seems also interesting feature to add to my application.

I don't know what your table is displaying, but as a computer user, do you want to stare at endless amounts of data? That just sounds like a design flaw to me, or a design that could be improved. I was suggesting some typical ways to reduce the size of tabular data being displayed:

1. Show just some of the data: perhaps the "latest" rows

2. Allow the user to constrain what is displayed: for example they could enter dates and you would only show rows that "fall between" those dates, etc, etc...

3. Page the display: show 30 rows at a time.

DrLaszloJamfa at 2007-7-8 23:26:52 > top of Java-index,Java Essentials,Java Programming...
# 9

> > can you explain me further this design proposition

> : it seems also interesting feature to add to my

> application.

>

> I don't know what your table is displaying, but as a

> computer user, do you want to stare at endless

> amounts of data? That just sounds like a design flaw

> to me, or a design that could be improved. I was

> suggesting some typical ways to reduce the size of

> tabular data being displayed:

>

> 1. Show just some of the data: perhaps the "latest"

> rows

> 2. Allow the user to constrain what is displayed: for

> example they could enter dates and you would only

> show rows that "fall between" those dates, etc,

> etc...

> 3. Page the display: show 30 rows at a time.

Yes I'm planning to add a filtering for data to be displayed on my table in next iterations of my application.

thank you the method of setting data vector for table model is working great.

think my problem is solved

Servant@AKa at 2007-7-8 23:26:52 > top of Java-index,Java Essentials,Java Programming...