I'm having trouble getting a JLabel
to display text immediately before updating it with an ImageIcon
.
I have a JLabel that is a fixed size that will be displaying either text or an image that fits it perfectly. I'm trying to get it to display the text "Loading..." and then immediately after, get it to load an image from a URL and replace the text with it.
...
public void setImage()
{
URL url = new URL("http://www.website.com/pathtoimage/image.png");
jLabel1.setIcon(null);
jLabel1.setText("Loading...");
ImageIcon ii = new ImageIcon(url);
jLabel1.setIcon(ii);
}
...
(Assume that exceptions are caught properly and all required imports are made, and that the image exists and is valid.)
The above code does not display the text; all that happens is the application hangs as the image is downloaded and displayed after a fraction of a second, mostly as expected. How can I force swing to update the JLabel before progressing on to replace it with the image? Or, do you know a different, more efficient way to achieve this?