This week's book giveaways are in the Java EE and JavaScript forums.
We're giving away four copies each of The Java EE 7 Tutorial Volume 1 or Volume 2(winners choice) and jQuery UI in Action and have the authors on-line!
See this thread and this one for details.
The moose likes Swing / AWT / SWT and the fly likes label(why is it not added?) Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of The Java EE 7 Tutorial Volume 1 or Volume 2 this week in the Java EE forum
or jQuery UI in Action in the JavaScript forum!
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "label(why is it not added?)" Watch "label(why is it not added?)" New topic
Author

label(why is it not added?)

codehouse
Greenhorn

Joined: Apr 11, 2002
Posts: 4
why doesn't the following code add a label?
public class LabelTester implements Runnable{
Label l=new Label("asdfd");
Thread t;
public void init()
{
add(l);
t=new Thread(this);
t.start();
}
public void run()
{
for(int i=0;;i++)
l.setText(String.valueOf(i));
}
}
what should it do according to u?
Rajendar Goud
Ranch Hand

Joined: Mar 06, 2002
Posts: 220
Hi ,
To add any component in AWT or swing ,u need to place them on a container.so u need to extend the Frame class to see the label.so the
code should be as follows,
//public class LabelTester implements Runnable
public class LabelTester extends Frame implements Runnable
and also
setSize(250,250);
setVisible(true);
to make the Frame visible.
cheers,
Raj
Nathan Pruett
Bartender

Joined: Oct 18, 2000
Posts: 4121

codehouse,

Please re-register with a name that follows the JavaRanch User Name Guidelines... your user name is required to be in the format "First Name" + space + "Last Name" and should not look obviously fake.

Since you are using "init()" it looks like you were trying to make an applet... you have to make your class extend Applet in orfer to do this. And you have to add your label to the applet to have it show up.

Here's some test code to show you how to do it... you can ignore the main method if you just want to run the applet from a web page...



-Nate


-Nate
Write once, run anywhere, because there's nowhere to hide! - /. A.C.
 
Consider Paul's rocket mass heater.
 
subject: label(why is it not added?)