Win a copy of Functional Reactive Programming this week in the Other Languages forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

How to force the repainting of a component

 
Jeronimo Backes
Greenhorn
Posts: 29
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi!

I made a JPanel that prints some text (and some other nice stuff) overriding paint(Graphics g) method. As I'm using text, I need to use FontMetrics to retrieve the bounds of my text and print it on the center.

But depending on the text size, and container size, it shows up a little twisted in the screen.

When I resize the window, the text appears as expected. And when I resize back to the original position, the text keeps showing correctly.

Is there a method in JComponent or JPanel that does the same kind of update as a manual resize does? I want to make my text to appear on screen accordingly since the beginning. How can I update my screen programmatically instead of "manually"?

I tried updateUI(), repaint() and invalidate() methods. And none of these made the job.

Any help will be appreciated.
Best Regards.
 
Ken Blair
Ranch Hand
Posts: 1078
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
repaint() is what you're looking for. If it's not working then that would indicate to me it's not a problem with the painting. Look at what the resizing does to affect your variants and it's probably somewhere in there.

If you post some code I'll be happy to try and help.
 
Craig Wood
Ranch Hand
Pie
Posts: 1535
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic