1

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.)?

Ertuğrul Çetin
  • 5,131
  • 5
  • 37
  • 76
  • You'll probably want to display only a portion of the data, and use a caching mechanism to control what is displayed. If there is one key term that you should search on it is cache and caching – Hovercraft Full Of Eels Sep 23 '18 at 18:04
  • Updates the Swing components and their models should be done on the EDT. `the UI because there is a lot of words to highlight (re-paint/re-render).` - you should only be highlighting the line of text that is changing. In this case even though the highlighting is done on the EDT it only affects one line instead of 10k and you won't have a problem with rendering. – camickr Sep 23 '18 at 21:38

1 Answers1

1

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/