Swing user interface not updating after using invokeLater

I have a Java Swing user interface that does not update / repaint as I thought. The application sends an XMPP message and receives a response to another stream. This response is processed, and the user interface is updated to reflect the information contained in the message.

When a response is received, I update the JPanel component using

javax.swing.SwingUtilities.invokeLater(new Runnable() { public void run() { /* execute logic to update panel */ } }); 

It has been quite a while since I developed it in Java, but based on my online research on invokeLater, queues are started to run in a GUI thread. However, my GUI is not updated until I do something else in the application that causes the redraw - for example, resizing the window. What am I missing? After the panel update logic, I tried various combinations of invalidate () and repaint (), but the result is the same - the GUI is not updated until, say, I resize the window.

EDIT: When I talk about updating the panel, I, in particular, do removeAll (), and then add some JLabels.

+7
java user-interface swing
source share
3 answers

After adding / removing components from the panel, you should use:

 panel.revalidate(); // this works 99% of the time panel.repaint(); // sometimes needed. 

Other things, such as validate (), invalidate (), etc., remain with AWT days, I believe, and revalidate () does a better job and is added specifically for Swing.

+14
source share

If you mean by β€œupdating a panel” that you add or remove, it is best to use invalidate () or revalidate () so that the layout manager catches the changes.

See here for a similar problem.

+2
source share

If you add or remove components from the panel while processing GUI events (for example, action listeners), call doLayout () to fix your problem. I found that the GUI is not updating, calling validate (), doLayout () and repaint () usually fixes the situation.

+2
source share

All Articles