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.

Add a Disabled Icon to a JButton Component in Java




















Home »
  Java Tutorial »
    Swing »




Action
Border
Color Chooser
Drag and Drop
Event
Font Chooser
JButton
JCheckBox
JComboBox
JDialog
JEditorPane
JFileChooser
JFormattedText
JFrame
JLabel
JList
JOptionPane
JPasswordField
JProgressBar
JRadioButton
JScrollBar
JScrollPane
JSeparator
JSlider
JSpinner
JSplitPane
JTabbedPane
JTable
JTextArea
JTextField
JTextPane
JToggleButton
JToolTip
JTree
Layout
Menu
Timer