The Java Developers Almanac 1.4


Order this book from Amazon.

   
Home > List of Packages > javax.swing  [141 examples] > JButton  [9 examples]

e751. Adding a Rollover and Pressed Icon to a JButton Component

The rollover icon is displayed when the cursor is moved over the button. If no rollover icon is set, the default icon is displayed when the cursor is moved over the button.

The pressed icon is displayed when the button is armed (i.e., the mouse button is down while the cursor is on the button). If no pressed icon is set, the default icon is displayed when the button is armed.

The rollover and pressed icons are never displayed if the button is disabled.

    // To create a button with an icon, see e746 Adding an Icon to a JButton Component
    
    // Add rollover icon
    Icon rolloverIcon = new ImageIcon("ricon.gif");
    button.setRolloverIcon(rolloverIcon);
    
    // Add pressed icon
    Icon pressedIcon = new ImageIcon("picon.gif");
    button.setPressedIcon(pressedIcon);
    
    // To remove rollover icon, set to null
    button.setRolloverIcon(null);
    
    // To remove pressed icon, set to null
    button.setPressedIcon(null);

 Related Examples
e743. Creating a JButton Component
e744. Changing the Label of a JButton Component
e745. Creating a Multiline Label for a JButton Component
e746. Adding an Icon to a JButton Component
e747. Moving the Icon in a JButton Component
e748. Moving the Label/Icon Pair in a JButton Component
e749. Setting the Gap Size Between the Label and Icon in a JButton Component
e750. Adding a Disabled Icon to a JButton Component

See also: Actions    JCheckBox    JComboBox    JDesktop and JInternalFrame    JFrame, JWindow, JDialog    JLabel    JList    JProgressBar    JRadioButton    JScrollPane    JSlider    JSpinner    JSplitPane    JTabbedPane    JToolBar    Keystrokes and Input Maps    Layout    Look and Feel    Menus    Progress Monitor    The Screen    Tool Tips    UI Default Values   


© 2002 Addison-Wesley.