Performance issue of JTable addcolumn()

2012年11月05日 19:59

 

When you try to add a lot of column to a JTable, say 100 or even more, it is not a reasonable way to call the addColumn() method directly. Because JTable treats different between the operation of rows and columns, it will work fine when you are continually calling the addRow() method 1000 times. But when it comes to the addColumn() method, it will triggers the TableModelListener every time you calling it. So, it is a sensible way to remove the listener when you start to add a batch of columns and set the listener back after finished.

 

 

// Adding a lot of columns would be very slow due to active model listener

tableModel.removeTableModelListener( table );



// Loop of call the addColumn() method

……



// After the Loop, set back the TableModelListener

tableModel.addTableModelListener( table );

// This is mandatory because it updates the table

tableModel.fireTableStructureChanged();