Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to force repaint in Swing while debugging? [duplicate]

I cannot seem to force a layout in Swing. I have a JComponent added to a JLayeredPane and I set a border on the JComponent. Then, I want to immediately re-draw everything - not in the sense of "please do this asap" like invalidate(), but synchronously and immediately. Any help? I cannot seem to find the right method of doing this, and all my reading about invalidate(), validate(), repaint(), doLayout(), etc is just confusing me more!

like image 314
Hamy Avatar asked Nov 19 '22 14:11

Hamy


2 Answers

According to this (see the section titled "Synchronous Painting") the paintImmediately() method should work.

like image 124
Jonathan Avatar answered Dec 21 '22 04:12

Jonathan


The most reliable way to get Swing to update a display is to use SwingUtilities.invokeLater. In your case, it would look something like

SwingUtilities.invokeLater(new Runnable {
                             public void run() { 
                                somecomponent.repaint();
                             }
                           });

I realize the 'invokelater' does not exactly sound like it does anything immediate, but in practice, events posted in this way tend execute pretty quickly compared to just calling e.g. somecomponent.repaint() directly. If you absolutely must make your control code wait for the GUI to update, then there is also invokeAndWait, but my experience is that this is rarely necessary.

See also: document on event dispatch in Swing.

like image 32
phooji Avatar answered Dec 21 '22 04:12

phooji