How To Set Your Icons On A Swing Button System

Swing Buttons provides many exclusive features that are not found in the Button class defined in the AWT package. Swing Buttons are the subclass of AbstractButton class,which extends JComponent. You can always associate an icon of your choice with a Swing Button. The Icons are set as program runs or set when some event is fired like button pressed. AbstractButton class contains many methods that allow you to control the behaviour of buttons,check boxes and Radio Buttons. There is the way you can set the icon on the swing button and it is through the JButton constructor.

Despite of being set by constructor we can change the Icon of our Button whenever some action is fired or some condition is met. We can define different Icons when button :-

1. is disabled
2. is pressed
3. is selected(for radio buttons etc)
4. gets mouse focus

JButton class is responsible for creation of all types of push buttons whether with icons or with text or both. The constructor used to create Button with Icon is

JButton(Icon i)
But if you also want to add Text with Icon then the constructor will be

JButton(String s ,Icon i)

In case you forgot to add text and you want to add text to the button whenever you want then use following method with JButton object.

setText(String str)

The following example will help in understanding how to set Icons and how you can change them according to your need.

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

public class ButtonImage extends JFrame implements ActionListener
JButton button;
public ButtonImage()
JFrame frm = new JFrame(“Icon on Button”);
JPanel panel = new JPanel();
ImageIcon imageicon = new ImageIcon(“d:\Anmtd_Feed. gif”);
ImageIcon im = new ImageIcon(“d:\snap. jpg”);
ImageIcon img = new ImageIcon(“e:\softwares\ICON gallery\Icons-1\128 x 128\apple1 (1). ico”);
button = new JButton(imageicon);
button. setText(“The Feed Icon”);
panel. add(button);
button. addActionListener(this);
button. setDisabledIcon(im);
button. setRolloverIcon(img);
frm. getContentPane(). add(panel);
frm. setVisible(true);
frm. setBounds(100,100,250,150);

public void actionPerformed(ActionEvent ae)
if (ae. getSource()==button)
button. setEnabled(false);

public static void main(String a[])
new ButtonImage();

This is first output Frame. In example it is shown that how we can change the button image when the button is disabled or whenever mouse moves over it.

The method setDisabledIcon(Icon i) and setRolloverIcon(Icon i) changes icons when the button is disabled or Hovered by Mouse respectively. ImageIcon is just a class which implements the Icon interface thus we are passing here ImageIcon as it is more convenient to use and most of all it implements Icon interface.

You can check on your own just change the Icon or Image paths then when the button is pressed the button is disabled and the image is changed once the button is disabled then there is no way you can change the icon of the button. That is why move mouse over the button first to test and then press the button.


Vaibhav Pandey
About Vaibhav Pandey
Vaibhav Pandey got offered employment from an Indian Multinational IT Company. He is 21 years old. He has a huge interest in Java programming and has liked it from his study days. Vaibhav loves to blog and share his experiences and thoughts. He now resides in Lucknow, a state capital in India. Check out his blog at

Leave a Reply

Your email address will not be published. Required fields are marked *