Two Laptop Bag
The moose likes Swing / AWT / SWT and the fly likes How to force the repainting of a component Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "How to force the repainting of a component" Watch "How to force the repainting of a component" New topic

How to force the repainting of a component

Jeronimo Backes

Joined: Sep 20, 2004
Posts: 29

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.

The art of being crazy is to NEVER commit the crazyness of being normal.
Ken Blair
Ranch Hand

Joined: Jul 15, 2003
Posts: 1078
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

Joined: Jan 14, 2004
Posts: 1535
I agree. Here's the link:
subject: How to force the repainting of a component
It's not a secret anymore!