Speed Swing Setup

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();
See also  5 Tips for Choosing the Right Medical Software for Your Practice

About Our Editorial Process

At DevX, we’re dedicated to tech entrepreneurship. Our team closely follows industry shifts, new products, AI breakthroughs, technology trends, and funding announcements. Articles undergo thorough editing to ensure accuracy and clarity, reflecting DevX’s style and supporting entrepreneurs in the tech sphere.

See our full editorial policy.

About Our Journalist