Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Cloud/Virtualization forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

label(why is it not added?)

 
codehouse
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 220
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 4121
IntelliJ IDE Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic