I have the following Applet class:
public class Marquee extends Applet {
Label label1 = new Label("Testing");
public void pushUpdate( String text ) {
System.out.println( "receiving: " + text );
label1.setText( text );
repaint();
}
public void init() {
ScrollText_2 scrollObj = new ScrollText_2( "Applet test" );
scrollObj.setApplet(this);
add( label1 );
scrollObj.run();
}
}
The ScrollText2 class implements Runnable and has a scroll() method. Right now, all the scroll method does is return the String that the object contains. The run() method of this class looks like this:
while(true) {
try {
marquee.pushUpdate( scroll() );
Thread.sleep( 2000 );
}
catch (InterruptedException e) {}
}
The problem is that when I run the applet, if I call the .run() method, then the label on the marquee never displays. I have tried repaint(), just label.setText(), updateUI(), and redraw() to try and get the applet to display the updates but it didn't work.
Any suggestions would be greatly appreciated.
Thank you!
You don't call run()
of a Thread or Runnable. You call start()
on the Thread or of a Thread that contains the Runnable. Also, you will need to take care to update the GUI components on the GUI's thread. For Swing that means using SwingUtilities.invokeLater(someRunnable)
, and I suspect it can be done similarly with AWT.
By the way, why use AWT and not Swing?