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(Log.now() + ": " + MESSAGE + "\n" + jTextArea1.getText());
               jTextArea1.setCaretPosition(0);
           }
       });
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));

5 comments:

  1. Ecorptrainings.com 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

    ReplyDelete
  2. I would like to appreciate your work for good accuracy and got informative knowledge from here................Please contact us for Oracle Fusion Financials training details in our Erptree Training Institute

    ReplyDelete
  3. The strategy you have posted on this technology helped me to get into the next level and had lot of information in it. please keep blogging...
    Tableau Training In Hyderabad
    Data science Training in Hyderabad

    ReplyDelete
  4. WordPress training in Hyderabad will help you develop your live website in just 15 Days.

    ReplyDelete
  5. This is the good website and also i love this website https://sclinbio.com

    ReplyDelete

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