Setting icon on the button in Java

This section illustrates you how to show the icon on the button in Java Swing.

Setting icon on the button in Java

This section illustrates you how to show the icon on the button in Java Swing.

 Setting icon on the button in Java

Setting icon on the button in Java

     

This section illustrates you how to show the icon on the button in Java Swing.

This program sets the icon on the button in Java Swing. 

Following is the output of the program:

Icon on Button

Code description:

setIcon(Icon):
Above method sets the specified Icon on the button.

ImagIcon(String image_name):
Above code creates an object of image.

Here is the code of the program:

import javax.swing.*;
import java.awt.*;

public class IconButton{
  public static void main(String[] args){
  JFrame frame = new JFrame("Icon on button");
  JButton button = new JButton("Roseindia.net");
  Icon imgicon = new ImageIcon("icon_confused.gif");
  JPanel panel = new JPanel();
  button.setIcon(imgicon);
  panel.add(button);
  frame.add(panel, BorderLayout.NORTH);
  frame.setSize(400400);
  frame.setVisible(true);
  frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  }
}

Download this example.