views:

32

answers:

1

I have a parent class which builds my UI (basically just a text box at the moment). I have created a secondary class which creates a new thread and I need to be able to update the textbox on the parent class from the new thread. Everything I try throws errors. I'm assuming I need to create some kind of dispatcher but my background is C# and I'm not 100% familiar on how to do this in Java.

My latest iteration passes the object of the parent class to a static method in the secondary which ultimately creates the new thread by instantiating an object for the secondary class (which has the run() method inside). I have a constructor in the secondary object that expects the passed in object from the parent class which, when passed in, sets a property that I've decalred in my member section for the parent class. when I try to access that member in the run() method to update a textbox on the parent class, I get an error.

Essentially the secondary class looks something like this:

        public class SecondaryClass extends Thread {
    private ParentClass pc = null;
    public SecondaryClass(ParentClass PC){
    pc = PC;
    }
    public static void StartThread(ParentClass pC){
       Thread thread = new SecondaryClass(pC);
       thread.start();
    }
    public void run() {
      pc.myTextBox.append("something");
    }
}

I've also tried creating a public method on the parent class that accepts a string as it's only argument and calling that method from within run() on the secondary class and passing "somethign" there. The method on the parent class updates the same textbox but having problems there as well.

can anyone please provide some insight as to what I need to do to access UI elements across these threads?

A: 

you will need to use SwingWorker

Similar question answered on stackoverflow for J2ME:

http://stackoverflow.com/questions/292587/interacting-with-ui-threads-in-java-j2me

Also read following:

http://java.sun.com/products/jfc/tsc/articles/threads/threads1.html

http://java.sun.com/products/jfc/tsc/articles/threads/threads2.html

Check simple example here:

http://download.oracle.com/javase/tutorial/uiswing/concurrency/worker.html

Hope this helps.

YoK
YoK, Thanks for the response. I'm actually writing this for android devices. I'm not sure if Swing or AWT works with android but I don't think it does. Is there a dispatcher method I can use instead of resorting to the swing package?
Kyle
@Kyle It should be supported.added link to similar question answered.
YoK
I just confirmed. Unfortunately swing is not supported in android development. I will mark your response as the answer since you did provide an adequate answer to my original question (not knowing that it was for android). I found this article which may help me resolve my issue for android development: http://www.ibm.com/developerworks/library/x-gourmetandroid/index.html?ca=dgr-lnxw97DalvikJavadth-JV
Kyle
Thanks Kyle for showing positive gesture. Most of the time users just let question remain unanswered in such situations.
YoK