Search This Blog

Friday, January 7, 2011

Java SWING GUI update

Today I stumbled upon a problem updating GUI SWING controls in java from a single threaded appliaction. Adding a simple invokeLate method did not work for me. jTextArea was still not updating properly. The code for updating text area looked like this:
       final String MESSAGE = newLine;
       SwingUtilities.invokeLater(new Runnable() {
           public void run() {
               jTextArea1.setText( + ": " + MESSAGE + "\n" + jTextArea1.getText());
I added a second thread and some Thread.sleep(10);'s between actions to give time for GUI thread to do its job. Unfortunately this gave me nothing. You have to create a Thread class:
public class CloneThread implements Runnable{
  private int index;
  private VMCloneInfo vmClInfo;
  public CloneThread(int index, VMCloneInfo vmClInfo) {
      this.index = index;
      this.vmClInfo = vmClInfo;
  public void run() {
      VBoxClonerView.getVBoxClonerView().cloneVM(index, vmClInfo);
and use ExecutorService. This is the key element for all this to work.
ExecutorService myExecutor = Executors.newCachedThreadPool();
myExecutor.execute((Runnable) new CloneThread(jComboBox1.getSelectedIndex(), vmCloneInfo));

1 comment:

  1. provides JAVA SWING in hyderabad with best faculties on real time projects. We give the best online trainingamong the JAVA SWING in Hyderabad. Classroom Training in Hyderabad India


If you like this post, please leave a comment :)