Is calling repaint from paintComponent a good practice

后端 未结 2 1946
孤街浪徒
孤街浪徒 2020-12-21 17:57

For some UI components in our application, we override paintComponent, which under some conditions \"recursively\" calls itself by invoking repaint

2条回答
  •  佛祖请我去吃肉
    2020-12-21 18:35

    There are very few situations where overriding paintComponent is a good thing. Your situation seems to be one of them; however, it is important to remember that it is not your job to call paintComponent. What I mean by this, is that it is an office of the System to decide when to repaint certain components. This is especially evident when you drag the screen around, or when you put another screen over yours. That being said, it is very difficult to say how many times your method will be called; therein, making it difficult to say when it would be worth using that implementation.
    On a side note, a background thread, as you put it, would more than likely not make it better, and Swing is notoriously not thread-safe.
    I hope this helps, and best of luck to you!

提交回复
热议问题