As you already mentioned, take care to always call Swing painting operations on the event dispatching thread by using SwingUtilities.invokeLater(Runnable)
or SwingUtilities.invokeAndWait(Runnable)
. Otherwise you'll get into trouble and have responsiveness issues that can ultimately lead to the so called 'Grey-Rect-Problem' where your frame is rendered as a grey rect and the UI does not respond anymore (keyboard, mouse events and so on).
Difference between invokeLater
and invokeAndWait
is that invokeLater
causes the java.lang.Runnable
you pass to it to be executed asynchronously on the AWT event dispatching thread. I don't know how you ensure that your painting operations are done on the EDT - so if your're not already using invokeLater
try this out first.
Other than that, as a general rule for optimisation of UI performance: Always try to minimize the area that has to be repainted! E.g. by using java.awt.Component.repaint(long tm, int x, int y, int width, int height)
, which repaints a specific area of a UI component in between a specified time.
Maybe these links also helps:
JTextArea setText(veryLongString) is taking too much time
https://pavelfatin.com/low-latency-painting-in-awt-and-swing/