Java Swing How to - Set pressed icon to JButton








Question

We would like to know how to set pressed icon to JButton.

Answer

//from   ww w.j  a v a2s .  c  o m
import javax.swing.Icon;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JOptionPane;
public class Main {
  public static void main(final String args[]) {
    JButton button = new JButton();
    Icon pressedIcon = new ImageIcon("pres-icon.gif");
    button.setPressedIcon(pressedIcon);

    JOptionPane.showMessageDialog(null, button);
  }
}