The Artima Developer Community
Sponsored Link

Java Answers Forum
Updating JLabel shows the preious text behind.

3 replies on 1 page. Most recent reply: Mar 24, 2005 4:39 AM by andy

Welcome Guest
  Sign In

Go back to the topic listing  Back to Topic List Click to reply to this topic  Reply to this Topic Click to search messages in this forum  Search Forum Click for a threaded view of the topic  Threaded View   
Previous Topic   Next Topic
Flat View: This topic has 3 replies on 1 page
andy

Posts: 6
Nickname: chant9
Registered: Mar, 2005

Updating JLabel shows the preious text behind. Posted: Mar 23, 2005 12:02 PM
Reply to this message Reply
Advertisement
Hi
I have a button that updates a JLabel. The problem is whenever i update the JLabel it simply writes over what was previously there and and you can see the previous text behind it.
Below is the code used.

jLabel8 = new JLabel(graph.getScale() * 100 + " %");

zoomPanel.add(jLabel8);
zoomPanel.validate();
zoomPanel.revalidate();
zoomPanel.repaint();

Does anyone know of a better way of doing it or is there a way of removing the previous text.

Any help will be apprieciated.

Thanks


Amol Brid

Posts: 43
Nickname: amolbrid
Registered: Feb, 2004

Re: Updating JLabel shows the preious text behind. Posted: Mar 23, 2005 9:34 PM
Reply to this message Reply
Hi,

No need to do validate()/revalidate()/repaint(). Just create a label and change its text in the action listener of button.
lblMsg = new JLabel("Old text");
.
.
.
// in button action listener
lblMsg.setText("New text");

Regards,
Amol Brid.

Matthias Neumair

Posts: 660
Nickname: neumi
Registered: Sep, 2003

Re: Updating JLabel shows the preious text behind. Posted: Mar 23, 2005 11:26 PM
Reply to this message Reply
Amol's solution is right.

But you surely want to know why your solution doesn't work, right?

The reason is, that jLabel8 is not a label, but a reference to a label

What happened?

1.) jLabel8 = new JLabel("text1);

This creates a new Label, let's call it labelA and puts a reference to it into jLabel8.

2.) zoomPanel.add(jLabel8);

This adds labelA to zoomPanel

3.) jLabel8 = new JLabel("text2);

This creates a new Label, let's call it labelB and puts a reference to it into jLabel8. But labelA is not affected by this operation.

4.) zoomPanel.add(jLabel8);

This adds labelB to zoomPanel. labelA still remains in zoomPanel.

andy

Posts: 6
Nickname: chant9
Registered: Mar, 2005

Re: Updating JLabel shows the preious text behind. Posted: Mar 24, 2005 4:39 AM
Reply to this message Reply
Thanks a lot for the help, its working now.

Cheers.

Flat View: This topic has 3 replies on 1 page
Topic: to DOM... Previous Topic   Next Topic Topic: Exporting to DBF

Sponsored Links



Google
  Web Artima.com   

Copyright © 1996-2019 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use