Swing: Update screen

Question: How do I update the screen?
In Java+Swing, I am re- JFrame.getContentPane().repaint() ing 60 times per second. However, the screen does not show all of my repaints: it slows down repainting when my mouse is not over the window.
How do I fix this?

I have tried:

// doesn't work:
SwingUtilities.updateComponentTreeUI(frame);
// annoyingly keeps the window focused:
frame.setVisible(true);
// doesn't work:
frame.invalidate();
frame.validate();
1 Like

Hi @JasonRoman I noticed you haven’t had any replies yet.

In order for the community to suggest ideas to help you solve your code issue it would be useful to share a link to your Repl.

Any error messages that you see appear when you attempt to run your code would also be useful to share here.

1 Like

I don’t use Java, but have you tried frame.revalidate?

There are no error messages when any of these are run.
Here is a link to the repl: @JasonRoman/FreakingMath
This repl uses frame.setVisible(true), because it’s the only one that I’ve found that works, even if it annoyingly prevents you from focusing any other windows.
I have tried frame.revalidate, and it doesn’t work.

Hi @JasonRoman thanks for the link.

I forked your Repl and tried to replicate the issue. Unfortunately I wasn’t able to get the slowdown you experienced. I’ve posted a video here so you can check and let me know if this is different to what you experience:

The slowdown only seems to happen when the mouse is not on top of the window, and it happens less in Replit than it does running it locally.
Also, currently the repl uses frame.setVisible(true). While this does fix the problem and prevent the slowdown, it also keeps the window focused, preventing any other windows from getting focused. (For instance, try right-clicking, then selecting “Fluxbox menu” and then “Fluxbox Command”. You won’t be able to type anything because the java window is constantly focusing itself.)

1 Like