Image rollover does not work

Can someone help me with the code please. I have two images 1.jpg and 2.jpg , when I run the program, the 1.jpg button appears on the button, but when I press the mouse button 2.jpg does not appear. Below is the code, thanks

 import javax.swing.*; class ButtonRollover { public static void main(String[] args) throws Exception { String path1 = ("C:\\1.jpg"); String path2 = ("C:\\2.jpg"); final JLabel pic1 = new JLabel(new ImageIcon(path1)); final JLabel pic2 = new JLabel(new ImageIcon(path2)); SwingUtilities.invokeLater(new Runnable() { public void run() { JButton button = new JButton("Hover"); button.setRolloverIcon(new ImageIcon("C:\\2.jpg")); button.add(pic1); button.setRolloverEnabled(true); JOptionPane.showMessageDialog(null, button); } }); } } 
+4
source share
1 answer

You should not add a shortcut inside the button. Just set its icon with

 button.setIcon(new ImageIcon(path1)); 

instead

 button.add(pic1); 
+2
source

All Articles