This week's book giveaway is in the Kotlin forum.
We're giving away four copies of Kotlin for Android App Development and have Peter Sommerhoff on-line!
See this thread for details.
Win a copy of Kotlin for Android App Development this week in the Kotlin 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
  • Liutauras Vilda
  • Devaka Cooray
  • Jeanne Boyarsky
  • Bear Bibeault
Sheriffs:
  • Junilu Lacar
  • Paul Clapham
  • Knute Snortum
Saloon Keepers:
  • Ron McLeod
  • Tim Moores
  • Stephan van Hulst
  • salvin francis
  • Carey Brown
Bartenders:
  • Tim Holloway
  • Frits Walraven
  • Ganesh Patekar

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.
 
It wasn't my idea to go to some crazy nightclub in the middle of nowhere. I just wanted to stay home and cuddle with this tiny ad:
Programmatically Create PDF Using Free Spire.PDF with Java
https://coderanch.com/wiki/703735/Programmatically-Create-PDF-Free-Spire
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!