Re: Thread static problem
Show the parts of the code in question.
Re: Thread static problem
Here is the code
Code:
import javax.swing.JPanel;
import java.awt.*;
public class TestBoard1 extends JPanel{
public TestBoard1(){
Runnable r = new Shuffler();
Thread t1 = new Thread(r);
t1.start();
}
public void Skriv(){
System.out.println("from thread");
}
}//End TestBoard1
class Shuffler implements Runnable{
public void run(){
try{
while(true){
TestBoard1.Skriv();
Thread.sleep(500);
}
}catch(InterruptedException iex){}
}//End run
}//End Class
The problem is the Skriv method
Re: Thread static problem
TestBoard1 is a class name, not an instance, so you are trying to call an instance method with a static reference.
Re: Thread static problem
I have changed the code so a instance "tt" of testboard1 is created, but this just gives me another question.
Now it gives me the Cannot find symbol error of the intance tt when i try to compile the tt.Skriv() in shuffler.
why can't the shufller class not find the "tt" instance when itself was created by that instance??
Code:
import javax.swing.JPanel;
import java.awt.*;
public class TestBoard1{
public TestBoard1(){
Runnable r = new Shuffler();
Thread t1 = new Thread(r);
t1.start();
}
public void write(){
System.out.println("from thread");
}
public static void main(String [] s){
TestBoard1 tt = new TestBoard1();
}
}//End TestBoard1
class Shuffler implements Runnable{
public void run(){
try{
while(true){
tt.write();
Thread.sleep(500);
}
}catch(InterruptedException iex){}
}//End run
}//End Class
Re: Thread static problem
Because tt is local to the main method. Add a constructor to your Shuffler class to pass it an instance of the TestBoard1 class and assign that instance to instance variable in the Shuffler class, then use that instance variable in the run method.