Jonathan Esquivel
Jonathan Esquivel

Reputation: 31

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);     
}                                

Upvotes: 1

Views: 114

Answers (1)

Jonathan Esquivel
Jonathan Esquivel

Reputation: 31

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"));

Upvotes: 1

Related Questions