Question:
Netbeans/Java: replace icon in JLabel...?
anonymous
2010-07-21 11:11:07 UTC
I have two images in my resources: A.png and B.png. A.png is set to display under normal conditions, by creating a new JLabel and inserting it into the icon under properties. Now I want to replace this image with B.png based on an if statement. This is what I have:
if (//certain file exists)
{
Icon icon = new ImageIcon("/images/B.png");
JLabel.setIcon(icon); //Here, JLabel is my JLabel where A.png is currently set as the default image.
}

When the if statement is true, the image A.png disappears and nothing shows up. Both images are the same size (in terms of dimensions). My guess is I have the wrong path for the image. I have tried...
/image/B.png
image/B.png
/B.png
B.png

Any help would be appreciated. Thank you in advanced!
Three answers:
Badajoz
2010-07-21 17:18:48 UTC
You might want to try calling the updateUI() method of JLabel right after you set the icon:



JLabel label = new JLabel();

label.setIcon(new ImageIcon("/image/B.png"));

label.updateUI(); //Add this one to your code.
?
2016-10-21 12:52:52 UTC
Netbeans Icon
anonymous
2016-03-18 04:50:21 UTC
Okay it's nearly 03.00hrs but I will correct some off yours mistakes . Yes St George has nothing to do with England , our patron St should be St Edmund or St Albuns . The St George thing only came over with the Normans in 1066 . There's a bit more to it than that but it's late and I can't be bothered . St George wasn't Turkish , he was a Roman soldier from the Eastern Roman Empire who was executed because he was a Christian . He lived in Asia minor . An are that is now called Turkey , at the time the Turks were in central Asia , so he wasn't Turkish . St Nick is also told to kids to be Turkish , yet again he lived in Asia Minor during a similar period and was probably ethnically Greek . Oh an by the way the Normans weren't French. We don't always agree on politics but my history is sound , don't twist it , study it .


This content was originally posted on Y! Answers, a Q&A website that shut down in 2021.
Loading...