Implementing a mouseover effect with a JButton

Add a MouseListener to your JButton and code the desired behaviour in mouseEntered and mouseExited.
This example makes the JButton’s label red when mouse-moving over it.

import java.awt.event.*;
import javax.swing.*;
import java.awt.*;
public class Main extends JFrame {
   JList list; 
   public Main() {
      getContentPane().setLayout(new FlowLayout());
      // following objects are final because they are used
      // in a inner class
      final JButton button = new JButton("OK!");
      button.addMouseListener(new MouseAdapter() {
         Color oldcolor = button.getForeground();
         public void mouseEntered(MouseEvent me) {
            oldcolor = button.getForeground();
         public void mouseExited(MouseEvent me) {
      addWindowListener(new WindowAdapter() {
         public void windowClosing(WindowEvent event) {
      setSize(400, 400);
   public static void main(String[] args) {
      (new Main()).show();