Search code examples
javaswingjscrollpane

refresh components in jframe in while loop


I have a table in database contain image paths
and inside the while loop on the query result , i added the images to JScrollPane

while (rs.next()) {///loop on the image paths from database 
 //// i added here the images to JScrollPane
}

but the frame is freezing until all the images set in JScrollPane then it shows to the user

what i need : show the images in JScrollPane one by one and sorted without freezing the frame?!


note : i used Thread and fixed the problem but there are a problem in sorted


Solution

  • Use SwingWorker to perform that task. Roughly speaking:

    In doInBackground method you will fetch images from database and publish them by calling publish method within doInBackground method. Override process method and update your JScrollPane from there. Since it runs on EDT where Swing components should be created and updated.