Win a copy of Kotlin in Action this week in the Kotlin forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Refresh  RSS feed

 
Rick Gentry
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???
 
Martina Renworth
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.
 
Craig Wood
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.
 
Daniel Searson
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.
 
Michael Dunn
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!