 |
AppletTalk.com Java discussions newsgroups
|
| View previous topic :: View next topic |
| Author |
Message |
Cameron Guest
|
Posted: Wed Jul 21, 2004 9:33 pm Post subject: Problem With Threads |
|
|
I posted a problem earlier under "Why doesn't this work," but I think
I've narrowed down the problem. I seem to be having some kind of
problem using threads. I guess it's just something basic I'm missing
about how they're used, but as far as I can tell I'm doing exactly
what the tutorials I've read are telling me to do. Basically, since I
thought the problem must lie with threads, I created a simple test
program that would display a jlabel through a thread. Sure enough, it
didn't work. Here's the code.
//<applet code=Test width=300 height=300>
//</applet>
import javax.swing.*;
import java.awt.event.*;
import java.awt.*;
public class Test extends JApplet{
private class TestThread extends Thread{
Container threadContainer;
public TestThread(Container aContainer) {
threadContainer = aContainer;
}
public void run() {
System.out.println("run()");
threadContainer.add(new JLabel("Test"));
}
}
Container theContainer;
TestThread theTestThread;
/** Creates a new instance of Test */
public Test() {
}
public void init() {
theContainer = getContentPane();
theTestThread = new TestThread(theContainer);
}
public void start() {
theTestThread.start();
}
}
Oddly enough, the System.out.println statement works fine, the jlabel
simply doesn't show up. Additionally, if I put an infinite loop in
the run() function, the System.out.println will print "run()"
infinitely without getting stuck (ie not closing on command), so the
thread itself seems to be working fine. However, something about the
thread seems to be preventing the jlabel from displaying. I'm pretty
sure it's the thread since putting theContainer.add(new
JLabel("Test")); in the init() function works fine. Even changing
theTestThread.start(); to theTestThread.run(); in the start() function
makes the jlabel display just fine.
I realize this may be a stupid question, but I don't think anything
I've read addresses this (the api documentation, my java book, random
internet turtorials, etc). I'm really frustrated by this, so any help
would be much appreciated. Thanks.
-Cam
|
|
| Back to top |
|
 |
Sebastian Scheid Guest
|
Posted: Thu Jul 22, 2004 8:55 pm Post subject: Re: Problem With Threads |
|
|
"Cameron" <camdog571 (AT) hotmail (DOT) com> schrieb im Newsbeitrag
news:4fe4079.0407211333.3523061e (AT) posting (DOT) google.com...
| Quote: | I posted a problem earlier under "Why doesn't this work," but I think
I've narrowed down the problem. I seem to be having some kind of
problem using threads. I guess it's just something basic I'm missing
about how they're used, but as far as I can tell I'm doing exactly
what the tutorials I've read are telling me to do. Basically, since I
thought the problem must lie with threads, I created a simple test
program that would display a jlabel through a thread. Sure enough, it
didn't work. Here's the code.
//
//
import javax.swing.*;
import java.awt.event.*;
import java.awt.*;
public class Test extends JApplet{
private class TestThread extends Thread{
Container threadContainer;
public TestThread(Container aContainer) {
threadContainer = aContainer;
}
public void run() {
System.out.println("run()");
threadContainer.add(new JLabel("Test"));
}
}
|
This should work:
public void run() {
System.out.println("run()");
SwingUtilities.invokeLater(new Runnable() {
public void run() {
threadContainer.add(new JLabel("Test"));
validate();
}
});
}
Read the doc for validate(). invokeLater() should always be used when
working with Swing Components from within a Thread you have started. See
http://www.javaworld.com/javaworld/jw-06-2003/jw-0606-swingworker.html for
more information.
| Quote: |
Container theContainer;
TestThread theTestThread;
/** Creates a new instance of Test */
public Test() {
}
public void init() {
theContainer = getContentPane();
theTestThread = new TestThread(theContainer);
}
public void start() {
theTestThread.start();
}
}
Oddly enough, the System.out.println statement works fine, the jlabel
simply doesn't show up. Additionally, if I put an infinite loop in
the run() function, the System.out.println will print "run()"
infinitely without getting stuck (ie not closing on command), so the
thread itself seems to be working fine. However, something about the
thread seems to be preventing the jlabel from displaying. I'm pretty
sure it's the thread since putting theContainer.add(new
JLabel("Test")); in the init() function works fine. Even changing
theTestThread.start(); to theTestThread.run(); in the start() function
makes the jlabel display just fine.
|
Sebastian
|
|
| Back to top |
|
 |
|
|
You cannot post new topics in this forum You cannot reply to topics in this forum You cannot edit your posts in this forum You cannot delete your posts in this forum You cannot vote in polls in this forum
|
|