Win a copy of Spring in Action (5th edition) this week in the Spring forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Bear Bibeault
  • Devaka Cooray
  • Liutauras Vilda
  • Jeanne Boyarsky
Sheriffs:
  • Knute Snortum
  • Junilu Lacar
  • paul wheaton
Saloon Keepers:
  • Ganesh Patekar
  • Frits Walraven
  • Tim Moores
  • Ron McLeod
  • Carey Brown
Bartenders:
  • Stephan van Hulst
  • salvin francis
  • Tim Holloway

Refresh  RSS feed

 
Ranch Hand
Posts: 63
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I've got my beautiful gui program all done except for one thing. How would I go about changing the text in a JLabel? jLabel.setText("whatever") changes the text I know, but it doesn't actually show the change. What do I do???
 
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
jLabel.repaint();
 
Rick Gentry
Ranch Hand
Posts: 63
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
doesn't work

I've got the label a few panels deep, so I don't know if that's affecting it or not. I even tried repainting everything in the whole tree leading down to the labels.
 
Ranch Hand
Posts: 1535
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Depends on how you put things together. If the new text changes the size of the JLabel you may need to call revalidate on the label.
 
Ranch Hand
Posts: 83
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Something that always works for me is:

invalidate()
validate()
repaint()

Sort of covers all of your bases. Hope that helps.

Normally, I would expect the JLabel to repaint itself when you call setText() so its surprising that you are having that problem actually...
 
Rick Gentry
Ranch Hand
Posts: 63
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
None of that's working. I'll post my code for someone to look at, but it's lengthy so I'll only do it if it's asked for.
 
Ranch Hand
Posts: 4632
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
>I'll post my code for someone to look at, but it's lengthy so I'll only do it if it's asked for.

post just enough (compilable) code to demonstrate the problem.

just a thought, you don't have a duplicate declaration, something like this -
run it as is, no change, comment out the duplicate declaration and it changes

 
Rick Gentry
Ranch Hand
Posts: 63
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Good idea, but that's no it either. The labels only have one instance of each. I have each of them inside an inner class extending JPanel, and those panels are in the JFrame.
 
Rick Gentry
Ranch Hand
Posts: 63
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks to those who helped, I found the bug. I wasn't resetting the text; I was reinstancing the labels. Dumb mistake.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!