Home | Ajax | BioInformatics | Dojo | EAI | EJB | Hibernate | J2ME | Java | Java Glossary | Java Servlets | JavaScript | Jboss | JDBC | JDO | Jmeter | JSF | JSP | JUnit | Maven | MySQL | Spring Framework | SQL | Struts | Technology | WAP | Web Services | XML
 
 
Search All Tutorials

 
Programming Tutorials: Ajax | Articles | JSP | Bioinformatics | Database | Free Books | Hibernate | J2EE | J2ME | Java | JavaScript | JDBC | JMS | Linux | MS Technology | PHP | RMI | Web-Services | Servlets | Struts | UML
 
Java
  JDO Tutorials
  EAI Articles
  Struts Tutorials
  Java Tutorials
  Java Certification
  Java Applet
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. 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.

                         

Facing Programming Problem?
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

Leave your comment:

Name:

Email:

URL:

Title:

Comments:


Enter Code:

 

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.

Hot Web Programming Job

Java String toLowerCase Example
Java String toCharArray Example
Java String substring Example
Java String indexOf Example
Java String startsWith Example
Java String hashCode Example
Java String matches Example
Java String length Example
Java String lastIndexOf Example
Java String isEmpty Example
Java String equalsIgnoreCase Example
Java String equals Example
Java String endsWith Example
Java String copyValueOf Example
Java String contentEquals Example
  EAI Articles
  Java Certification
Tell A Friend
Your Friend Name
Search Tutorials

 

 
 
Browse all Java Tutorials
Java JSP Struts Servlets Hibernate XML
Ajax JDBC EJB MySQL JavaScript JSF
Maven2 Tutorial JEE5 Tutorial Java Threading Tutorial Photoshop Tutorials Linux Technology
Technology Revolutions Eclipse Spring Tutorial Bioinformatics Tutorials Tools SQL
 

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

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

Copyright © 2007. All rights reserved.