简体   繁体   中英

Is Java Swing blocking UI while painting/rendering?

I'm writing an editor and I have some performance issues. The thing is when there is a lot of text (10K Lines) in the editor Swing blocks (gets really slow) the UI because there is a lot of words to highlight (re-paint/re-render).

I'm also using EDT (Event Dispatch Thread).

Does swing block UI when painting/rendering? Is there any way to optimize rendering while I type some words to the editor (like async painting etc.)?

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! Eg 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/

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM