Reputation: 41832
I have a Java Swing UI that isn't updating/repainting as I thought it should. The app sends an XMPP message and receives a response on a different thread. That response is processed and the UI is updated to reflect information contained in the message.
When the response is received, I update a JPanel component using
javax.swing.SwingUtilities.invokeLater(new Runnable() {
public void run() { /* execute logic to update panel */ }
});
It's been quite sometime since I've developed in Java, but based on my research online invokeLater queues the runnable up for execution on the GUI thread. However, my GUI doesn't update until I do something else in the app that causes a repaint - such as resizing the window. What am I missing? After the logic for updating the panel, I've tried various combinations of invalidate() and repaint(), but the result is still the same - the GUI does not update until I, say, resize the window.
EDIT: When I say updating the panel, I am, specifically, doing a removeAll() and then adding a handful of JLabels.
Upvotes: 7
Views: 5671
Reputation: 324118
After adding/removing components from a panel you should use:
panel.revalidate(); // this works 99% of the time
panel.repaint(); // sometimes needed.
Other stuff like validate(), invalidate() etc., is left over from AWT days I believe and revalidate() does a better job and was added specifically for Swing.
Upvotes: 14
Reputation: 8575
If you are adding or removing components from a panel during the GUI event handling (such as action listeners) calling doLayout() should hopefully fix your problem. I've found when the GUI doesn't update, calling validate(), doLayout(), and repaint() tends to fix things.
Upvotes: 2
Reputation: 4356
If you mean by "update the panel" that you are adding or removing components, then you better use invalidate() or revalidate() so that the layout manager catches the changes.
See here for a similar problem.
Upvotes: 2