Add a Disabled Icon to a JButton Component in Java
Description
The following code shows how to add a Disabled Icon to a JButton Component.
Example
// ww w. j a va 2 s . com
import javax.swing.Icon;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
public class Main{
public static void main(String[] a){
JFrame f = new JFrame();
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JButton button = new JButton();
button.setEnabled(false);
f.add(button);
// Set a disabled version of icon
Icon disabledIcon = new ImageIcon("d.gif");
button.setDisabledIcon(disabledIcon);
// To remove the disabled version of the icon, set to null
//button.setDisabledIcon(null);
f.setSize(500,500);
f.setVisible(true);
}
}
The code above generates the following result.
Home »
Java Tutorial »
Swing »
Java Tutorial »
Swing »