Question

How can I make the change of an existing JLabel with Null ICON to a certain icon after a set of conditions... My logic was for sure clear, but maybe my sintaxis is the problem, please aid.

    if(n==true){
    trofeo1.setIcon(trofeo.png);
    }

My JLabel is trofeo1, n is just an example of my logic..

Thanks!

Was it helpful?

Solution

In your code, try this:

if (n == true) {
    SwingUtilities.invokeLater(new Runnable() 
    {
        public void run() 
        {
            trofeo1.setIcon(new ImageIcon("trofeo.png"));
        }
    });
}

Maybe you can check first the image file is right by doing something like this:

File f = new File("trofeo.png");
if (f.exists()) {
} else {
}

This is because many times the problem is the image location.

Then, you say "maybe my sintaxis is the problem...", does your code compile? Have you put a breakpoint at trofeo1.setIcon() line and check this line is executed? I assume the answer to these two questions is true.

Regards,

OTHER TIPS

Did you try to refresh your JLabel with the method repaint()?

 if(n==true){
    trofeo1.setIcon(new ImageIcon("trofeo.png"));
    trofeo1.repaint();
 }

This method is an update of the component's display.

For those who end up here, yea, MY LINK or path to the photo was wrong, just go look for the photo location throught your carpets, copy the url and it will be ready to go. Here is an example....

C:\Users\Beto\Documents\NetBeansProjects\ProyectoFinal\src\proyectofinal\trofeo.png

Thanks to all who tried to help!

Final code

if(n==true){
trofeo1.setIcon(new ImageIcon("C:\Users\Beto\Documents\NetBeansProjects\ProyectoFinal\src\proyectofinal\trofeo.png"));
trofeo1.repaint();
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top