- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
How to implement a rollover effect for a JButton in Java?
A JButton is a subclass of AbstractButton and it can be used for adding platform-independent buttons to a GUI application. A JButon can generate an ActionListener interface when the button is pressed or clicked, it can also generate the MouseListener and KeyListener interfaces. We can implement the rollover effect when the mouse moves over a JButton by overriding the mouseEntered() method of the MouseListener interface.
Syntax
void mouseEntered(MouseEvent e)
Example
import java.awt.*; import java.awt.event.*; import javax.swing.*; public class RollOverButtonTest extends JFrame { private JButton button; public RollOverButtonTest() { setTitle("RollOverButton Test"); button = new JButton("Rollover Button"); button.addMouseListener(new MouseAdapter() { Color color = button.getForeground(); public void mouseEntered(MouseEvent me) { color = button.getForeground(); button.setForeground(Color.green); // change the color to green when mouse over a button } public void mouseExited(MouseEvent me) { button.setForeground(color); } }); add(button, BorderLayout.NORTH); setSize(400, 300); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); setLocationRelativeTo(null); setVisible(true); } public static void main(String[] args) { new RollOverButtonTest(); } }
Output
- Related Articles
- How can we implement the HTML text of JButton in Java?
- How to add empty border to a JButton in Java?
- How can we set the margin to a JButton in Java?
- How to add Icon to JButton in Java?
- How can we set the shortcut key to a JButton in Java?
- How can we add/insert a JButton to JTable cell in Java?
- How to change JButton font dynamically in Java?
- How to add action listener to JButton in Java
- How to set action command to JButton in Java
- How can we apply different borders to JButton in Java?
- How to add shadow Effect for a Text in Android?
- I want to call JButton doClick() method to simulate a click action in Java
- How can I show image rollover with a mouse event in JavaScript?
- How to implement a String in JShell in Java 9?
- How to implement a program to count the number in Java?

Advertisements