Thursday, July 8, 2010

How to Set Icons on Swing Buttons  


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 :- disabled pressed 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");

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

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.

What next?

You can also bookmark this post using your favorite bookmarking service:

Related Posts by Categories

Extension Factory Builder