简体   繁体   中英

JComponent: how to do a repaint in the most correct way?

We have following methods:

  • java.awt.Component#repaint()

    This is old method that repaints in AWT. It does not repaint immediately, it schedules repaint.

  • javax.swing.JComponent#repaint(long, int, int, int, int)

    This is new swing method that repaints in Swing. It schedules repaint with the RepaintManager.

Both of them may be invoked from a user thread as well as from the event-dispatching-thread.

Which of them is the most correct way to repaint Swing component ( JComponent )?

Which of them is the most correct way to repaint Swing component (JComponent)?

Both of them are the correct way.

Swing components always invoke repaint() when you change a property of the component by using setText(), setForeground(), setBackground() etc. This will make sure the entire component is repainted.

The repaint(...) method can be invoked if you need to optimize painting of the component. If you have a large component and only a small part of the component changes you can use this method. However, I would recommend you don't worry about this. Swing painting is double buffered and efficient so there is rarely a case when you need to optimize the painting code.

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