Adding an Icon to a JButton Component

If the action used to create the button contains an icon, the button will be created using that icon. The icon will appear to the left of the text; to change the icon's position, see Moving the Icon in a JButton Component.
// Retrieve the icon Icon icon = new ImageIcon("icon.gif"); // Create an action with an icon Action action = new AbstractAction("Button Label", icon) { // This method is called when the button is pressed public void actionPerformed(ActionEvent evt) { // Perform action } }; // Create the button; the icon will appear to the left of the label JButton button = new JButton(action);
If the action does not have an icon or a different icon must be used, add or change the icon using setIcon():
// Add or change the icon; it will appear to the left of the text button.setIcon(icon); // Set to null to remove icon button.setIcon(null);

Comments

7 Jun 2010 - 12:07pm by Anonymous (not verified)

thanks!!

22 Jun 2010 - 12:26am by Anonymous (not verified)

thanks

3 Sep 2010 - 12:42am by tudor watches (not verified)

Billionaire Dior clothing Donald Bren Left Children 'Very franck muller watch Angry and Very Hurt' After richard mille watches Trial, Ex Girlfriend Says
Jennifer Gold gucci for women Told 'GMA' That Lawsuit to Nike clothing Recoup Millions 'Not About Greed'
By Christian Dior wallet MIKE VON FREMD, SARAH NETTER Nike zoom flight 95 and SHERYL NANCE-NASH

Post a comment

More information about formatting options

CAPTCHA
This question is for testing whether you are a human visitor and to prevent automated spam submissions.
Image CAPTCHA
Enter the characters shown in the image. Ignore spaces and be careful about upper and lower case.