Core Java| JSP| Servlets| XML| EJB| JEE5| Web Services| J2ME| Glossary| Questions?

 

 

 

 

 

 

 

 

 

 

 

 

 

Search Tutorials

Latest Questions
Comments
 
Adding a Rollover and Pressed Icon to a JButton Component in Java 
 

Here, you will learn about adding event i.e. the rollover and click icon to a JButton component of swing in java.

 

Adding a Rollover and Pressed Icon to a JButton Component in Java

                         

Here, you will learn about adding event i.e. the rollover and click icon to a JButton component of swing in java. Rollover means moving mouse pointer above the icon on the button.  This program shows an icon or image on the button if the mouse pointer moves above the Button then your icon or image should be changed. When you click on the button then another image or icon should be shown on the button.

This program displays a button on a frame. Button shows different icons like: cut, copy and paste on different events. At first, the button shows the "cut" icon and when the mouse pointer moves above the button then the button shows the "copy" icon and when you click on the button then the "paste" icon is seen. Following are the screenshot of the application:

This the "cut" image which occurs by default when the program is rum from the command prompt.
Roll Over : Cut Image (By Default)

This is the "copy" image which occurs by default when user will rollover the image or button.
Roll Over : Copy Image (When the image is rollovered

And this is the "paste" image which occurs by default when user clicks on the button.
Roll Over : Paste Image (When the image is clicked)

Code Description:

These events are managed by the program using some APIs or methods as follows:

button.setRolloverIcon(Icon icon_name):
This is the method of the JButton class which is used to set the icon or image to a button for display when the mouse pointer rolls over the icon or the button. The icon or image is passed through the method as a parameter.

button.setPressIcon(Icon press):
This is the method of the JButton class which is used to set the icon or image to a object for displaying when the button is clicked. The icon or image is specified in the method argument as a parameter.

Here is the code of program:

import javax.swing.*;
import java.awt.*;

public class RolloverComponent{
  public static void main(String[] args) {
    JFrame frame = new JFrame("Adding a Rollover and Pressed Icon 
to a JButton Component"
);
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    JPanel panel = new JPanel();
    frame.add(panel,BorderLayout.CENTER);
    JButton cutbutton = new JButton(new ImageIcon("cut.gif"));
    panel.add(cutbutton);
    ImageIcon rollover = new ImageIcon("copy.gif");
    cutbutton.setRolloverIcon(rollover);
    ImageIcon press = new ImageIcon("paste.gif");
    cutbutton.setPressedIcon(press);
    frame.setSize(400,400);
    frame.setVisible(true);
  }
}

Download this example.

                         

» View all related tutorials
Related Tags: java c ide class graphics constructor object io struct classes size 2d graph const ip vi trac awt int this

Leave your comment:

Name:

Email:

URL:

Title:

Comments:


Enter Code:

Audio Version
Reload Image
 

Note: Emails will not be visible or used in any way, and are not required. Please keep comments relevant. Any content deemed inappropriate or offensive may be edited and/or deleted.

No HTML code is allowed. Line breaks will be converted automatically. URLs will be auto-linked. Please use BBCode to format your text.

Add This Tutorial To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 

Current Comments

2 comments so far (
post your own) View All Comments Latest 10 Comments:

how do i change the label of JBUTTON to show up arrow

Posted by sumeet on Friday, 05.4.07 @ 15:28pm | #15262

Y isnt that the images arent working for me.i tried to specify an image of my choice and it wasnt working.wat is the pblm...?

Posted by pradeesh on Wednesday, 04.25.07 @ 16:02pm | #14913

Training Courses
Tell A Friend
Your Friend Name
Software Solutions
Least Viewed
Most Rated
Recently Viewed
Search Tutorials

 

 
 

Home | JSP | EJB | JDBC | Java Servlets | WAP  | Free JSP Hosting  | Search Engine | News Archive | Jboss 3.0 tutorial | Free Linux CD's | Forum | Blogs

About Us | Advertising On RoseIndia.net  | Site Map

India News

Indian Software Development Company | iPhone Development Company in India | Flex Development Company in India | Java Training Delhi | Java Training at Noida |

Send your comments, Suggestions or Queries regarding this site at roseindia_net@yahoo.com.

Copyright © 2008. All rights reserved.