Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Force immediate layout and paint in Swing

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 983
Hamy Avatar asked Jan 21 '11 16:01

Hamy


People also ask

What is system triggered painting?

In a system-triggered painting operation, the system requests a component to render its contents, usually for one of the following reasons: The component is first made visible on the screen. The component is resized. The component has damage that needs to be repaired.

When a JComponent is displayed its paint method is called?

The painting method you're most likely to override is paintComponent . It's one of three methods that JComponent objects use to paint themselves. The three methods are invoked in this order: paintComponent — The main method for painting. By default, it first paints the background if the component is opaque.

How do you paint a swing?

In Java Swing, we can change the paintComponent() method instead of paint() method as paint calls paintBorder(), paintComponent() and paintChildren() methods. We cannot call this method directly instead we can call repaint(). repaint(): This method cannot be overridden. It controls the update() -> paint() cycle.

What is difference between repaint and update method?

update(): update is called when the window is re-sized. The default implementation of update(): first clears the background; then calls paint(). repaint(): The repaint() is intended to allow various methods to call for a re-rendering of the component. No graphics context is needed for repaint().


2 Answers

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

like image 179
Jonathan Avatar answered Oct 18 '22 15:10

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 Oct 18 '22 17:10

phooji