Home > Java > Changing the colour of a JButton on ‘mouse hover’

Changing the colour of a JButton on ‘mouse hover’

While working on i project my UI conscious client wanted that the buttons(JButton) changes its colour when the mouse is hovered over it.
Then i wrote this custom version of JButton, the ‘PosButton’ that create a button whose default color and mouse over colour can be specified at the time of object creation i.e. new PosButton(String text, Color defaultColor, Color ouseOverColor)….
I felt that this code can be useful
for many, so i am posting it….

to use the code just copy it and save in a file named PosButton.java, and use this custom button. Here goes the code:

import javax.swing.JButton;
import java.awt.Color;
import java.awt.event.MouseListener;
import java.awt.event.MouseEvent;

public class PosButton extends JButton implements MouseListener { 

//instance variables

private Color defaultColor;
private Color mouseOverColor;

//class constructor

public PosButton(String text, Color defaultColor, Color mouseOverColor) { 



this.defaultColor = defaultColor;

this.mouseOverColor = mouseOverColor;



//override the methods of implemented MouseListener

public void mouseClicked(MouseEvent e) { }

public void mousePressed(MouseEvent e) { }

public void mouseReleased(MouseEvent e) { }

public void mouseEntered(MouseEvent e) { 

if(e.getSource()==this) {  this.setBackground(this.mouseOverColor); }


public void mouseExited(MouseEvent e) { 

if(e.getSource()==this) { this.setBackground(this.defaultColor); }



And to test the PosButton, here’s MyFrame.java

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

public class MyFrame extends JFrame {

//instance attributes

private PosButton pb;

//class constructor

public MyFrame() {

super("Button colour change demo");

setLayout(new FlowLayout());


//instantiate PosButton class

pb = new PosButton("Button by Hassan", new Color(255, 125, 64), new Color(192, 255, 62));




//main method

public static void main(String[] args) { 

MyFrame mf = new MyFrame();



Categories: Java
  1. September 20, 2010 at 7:04 pm

    Hey Hassan,

    Dude, I’m lovin’ your work.
    Wish you best.

    Just saw your recent examples in the posts.
    You have copied and pasted all the code of your projects.
    Don’t do that.
    Add files of any particular project to a ZIP archive of that name(ZIP for cross-platform support).
    And provide a download link for them.

    Otherwise your post will take huge space and look messy.
    Copying will be a problem for others.

    Hope you like my suggestion.


    • September 21, 2010 at 9:07 am

      Hello Haseeb, thanx for your appreciation for the work:-)
      Your appreciation is a thing that makes me smile during chaotic moments..

      The thing you pointed out is a thing i am trying to fix out these days, but i was afraid of the poor reliability of the sites offering file uploading services(your file may be deleted any time after uploading), but anyhow i am seeking some good uploading service, and soon that would be fixed..Thanx again for your suggestion, these are the only things that would make the blog better.

  1. No trackbacks yet.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: