Posts

Showing posts from July, 2011

How to asynchronously update GUI in Java

Image
There are times an application needs to display feedback to the user that certain activity is going on like status of a progress perhaps. In Java an application cannot update the GUI asynchronously while you're also processing using the same thread. What normally happens is that your GUI will be updated only after the processing have completed which is not we want. So, to do what we want we need to use only the main application thread for GUI update while we will create a separate thread for processing in that way the GUI is updated asynchronously. Sample Code: public class ProgressDialog extends javax.swing.JDialog{     public ProgressDialog(java.awt.Frame parent, boolean modal) {         super(parent, modal);         initComponents();         worker = new Worker();     } private void StartActionPerformed(java.awt.event.ActionEvent evt) {         if(Start.getText().equals("Start"))         {             worker.Start();             Start.setText("St