Java Swing How to - Create JButton from Icon








Question

We would like to know how to create JButton from Icon.

Answer

/*from  ww w.  j  ava  2  s. com*/
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[]) {
    Icon icon = new ImageIcon("dog.jpg");
    JButton button = new JButton(icon);


    JOptionPane.showMessageDialog(null, button);
  }
}