How to asynchronously update GUI in Java
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("Stop");
}
else
{
worker.Stop();
Start.setText("Start");
}
}
public static void main(String args[]) {
ProgressDialog dialog = new ProgressDialog(new javax.swing.JFrame(), true);
dialog.setVisible(true);
}
private javax.swing.JButton Start;
private javax.swing.JLabel jLabel1;
private javax.swing.JProgressBar jProgressBar1;
private boolean increment;
private Worker worker;
class Worker
{
Thread worker;
Worker()
{
worker = new Thread(new Runnable() {
public void run() {
int value=0;
while(increment)
{
try {
jProgressBar1.setValue(value);
Thread.sleep(100);
value++;
} catch (InterruptedException ex) {
Logger.getLogger(ProgressDialog.class.getName()).log(Level.SEVERE, null, ex);
}
if(value>100) break;
}
Start.setText("Start");
}
});
}
public void Start()
{
increment=true;
worker.start();
}
public void Stop()
{
increment=false;
}
}
}
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("Stop");
}
else
{
worker.Stop();
Start.setText("Start");
}
}
public static void main(String args[]) {
ProgressDialog dialog = new ProgressDialog(new javax.swing.JFrame(), true);
dialog.setVisible(true);
}
private javax.swing.JButton Start;
private javax.swing.JLabel jLabel1;
private javax.swing.JProgressBar jProgressBar1;
private boolean increment;
private Worker worker;
class Worker
{
Thread worker;
Worker()
{
worker = new Thread(new Runnable() {
public void run() {
int value=0;
while(increment)
{
try {
jProgressBar1.setValue(value);
Thread.sleep(100);
value++;
} catch (InterruptedException ex) {
Logger.getLogger(ProgressDialog.class.getName()).log(Level.SEVERE, null, ex);
}
if(value>100) break;
}
Start.setText("Start");
}
});
}
public void Start()
{
increment=true;
worker.start();
}
public void Stop()
{
increment=false;
}
}
}
Hey, I'm trying to create a GUI, but there is a problem with event dispatch thread. So I'm looking for asynchronous calls, but your code doesn't work. There is not initComponents() method for example. Can you please expand the example?
ReplyDeleteinitComponents() is just initializing the controls.. By the way I used netbeans on this example and I assumed that you already know basic java
Delete