Win a copy of The Java Performance Companion this week in the Performance forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

How do you "delete" a JLabel before adding a JLabel on top?

 
Andrew Brown
Greenhorn
Posts: 25
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
i am writing an aplet that changes certain JLabels that i have created when a button is clicked, when i change the label it simply over writes the old label witht the new, which would be ideal, but i can still see the JLabel text from the previous Label that has been laid down.

Any ideas?
 
Paul Clapham
Sheriff
Posts: 21126
32
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If you want to change the text in a JLabel, why wouldn't you just call its setText(String) method? Trying to delete a JLabel and then add a new one in the same place strikes me as being the hard way to do things.
 
Andrew Brown
Greenhorn
Posts: 25
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
again i have the same problem. At the moment i have a JLabel(na), where na is a string and i change the string and print it out over the top using updateUI(), but i can still see the text that was there previous, and i don't want to be able to.

When i changed the text using the suggested setText(); method, it had the same effect.
 
Michael Dunn
Ranch Hand
Posts: 4632
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
> When i changed the text using the suggested setText(); method, it had the same effect.

your applet could be using a cached .class file i.e. not using the updated file

copy the .java file into another directory, recompile and run it again.
 
Andrew Brown
Greenhorn
Posts: 25
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This is not the case as i have recompilerd it in a different directory and it still writes the letters over the top, leaving me with nothig but giberish.

I have also tried calling the back ground to be white so that it is still there but you simply can't see it, but this did not work either.
 
Michael Dunn
Ranch Hand
Posts: 4632
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
probably get your problem solved a lot quicker if you post the code you are using
 
Andrew Brown
Greenhorn
Posts: 25
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
nb = "null";

bNotesCanvas = new JLabel(nb);

then a separate method saying:

if(event.getSource() == convertButton){
refresh();
}

refresh being:

public void refresh(){
nb = "a";
bNotesCanvas.updateUI();
}

Sorry, didn't think about that.
 
Jared Cope
Ranch Hand
Posts: 243
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Andrew Brown:
nb = "null";

bNotesCanvas = new JLabel(nb);
...
...
public void refresh(){
nb = "a";
bNotesCanvas.updateUI();
}



Hi,

I would change the line

with


I don't think changing the value of (what I assume is a String variable) nb propagates to the JLabel (even though you have passed that into the constructor). I believe the constructor will use nb as a 'value' rather than a 'reference'.

Cheers, Jared.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic