Login | Register   
LinkedIn
Google+
Twitter
RSS Feed
Download our iPhone app
TODAY'S HEADLINES  |   ARTICLE ARCHIVE  |   FORUMS  |   TIP BANK
Browse DevX
Sign up for e-mail newsletters from DevX


Tip of the Day
Language: Java
Expertise: Intermediate
Feb 17, 1998

Speed Swing Setup

In Swing 0.7, and probably later versions as well, the DefaultListModel class notifies its ListDataListeners every time a new element is added to the list model. For very large lists this can result in extremely slow JList updates. To work around the issue, set the JList's model to null before updating the list, then reset the JList's model when you're through. This way no expensive event signaling is performed, speeding up your list update. Don't forget to repaint and revalidate the JList, because a JList does not update its appearance after setModel() is called.

A short demonstration follows. Note that we actually clear the list and refill it with new data, which requires making sure the selection gets cleared and that the top of the list is visible.

 
list.setModel(null);
listModel.removeAllElements();
for(int i=0; i < files.length; i++) {
	if(!files[i].isDirectory())
		listModel.addEelment(files[i]);
}
list.setModel(listModel);
list.clearSelection();
list.ensureIndexIsVisible(0);
list.repaint();
list.revalidate();
Daniel F.
 
Comment and Contribute

 

 

 

 

 


(Maximum characters: 1200). You have 1200 characters left.

 

 

Sitemap
Thanks for your registration, follow us on our social networks to keep up-to-date