Search code examples
buttonnetbeansiconsjlabelimageicon

JLabel Icon won't change within package images (Netbeans)


So I have this two images(.png) on my app package, and I want to change a JLabel's icon A to Icon B once the user clicks the original icon. The problem is, once the label i clicked, the image disappears and a text show's up instead ("jLabel1"). Here's the code for the listener:

private void login_IngresarBMouseClicked(java.awt.event.MouseEvent evt){                                             
    String imageName = "login_buttonHighlight.png";
    ImageIcon icon = new ImageIcon(imageName);
    icon.getImage().flush();
    login_IngresarB.setIcon(icon);     
}                                

Solution

  • Nevermind, I found the answer, used getResourse() instead of just the image name:

    String imageName = "login_buttonHighlight.png";
    ImageIcon icon = new ImageIcon(imageName);
    

    For this one:

    ImageIcon nuevo = new ImageIcon(getClass().getResource("login_buttonHighlight.png"));