Forums Register Login

How to force the repainting of a component

+Pie Number of slices to send: Send
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.
+Pie Number of slices to send: Send
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.
+Pie Number of slices to send: Send
What are you doing? You are supposed to be reading this tiny ad!
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com


reply
reply
This thread has been viewed 4179 times.
Similar Threads
JPanel - Screen Size
Geting Panel Size from Container
Spawing operations off to separate threads Vs executing on the EDT
JFrame not displaying text.
same old gridbag
More...

All times above are in ranch (not your local) time.
The current ranch time is
Mar 28, 2024 07:22:55.