Results 1 to 2 of 2
  1. #1
    new2java2009 is offline Member
    Join Date
    Aug 2009
    Rep Power

    Default button background color

    I have a button on a frame, which I like the background color to behave as follow with the mouse:
    1. When the mouse is over the button, I like it to be "buttonHighlightColor".
    2. when the mouse is pressed (hold, not release yet), I like the color to be "buttonDownColor".
    3. When the mouse is release, but the mouse is still on the button, I like the color to be "buttonHighlightColor".
    4. When the mouse leaves the button, I like the color to change to "buttonUpColor".

    I used the following code, everything works fine, but 2 doesn't work.

    I expect that while I am holding my mouse (mousePressed), the button color would be "buttonDownColor", but it's not. It's the default light blue color.

    Can someone tell me what I am doing wrong, and how to fix it? Following is my code.


    Java Code:
    import java.awt.Color;
    import java.awt.event.MouseEvent;
    import java.awt.event.MouseListener;
    import javax.swing.JButton;
    import javax.swing.JFrame;
    class MouseTest {
    	public static void main(String[] args) {
    		MouseListener popup;
    		final Color buttonDownColor = new Color(72, 63, 48);
    		final Color buttonHighlightColor = new Color(112, 98, 72);
    		final Color buttonUpColor = new Color(79, 70, 54);
    		JFrame frame = new JFrame();
    		final JButton button = new JButton("Test How The Mouse Works");
    		popup = new MouseListener() {
    			public void mouseClicked(MouseEvent e) {
    		     public void mouseEntered(MouseEvent e) {
    		     public void mouseExited(MouseEvent e) {
    		     public void mousePressed(MouseEvent e) {
    		     public void mouseReleased(MouseEvent e) {
    		frame.setSize(300, 300);

  2. #2
    camickr is offline Senior Member
    Join Date
    Jul 2009
    Rep Power

    Default Re: button background color

    The ButtonUI does some custom painting that ignores the background color when the button is pressed.

    Change you code as follows to see how it works for a JLabel:
    Java Code:
    //        final JButton button = new JButton("Test How The Mouse Works");
            final JLabel button = new JLabel("Test How The Mouse Works");

Similar Threads

  1. How to change background color
    By Jeffrey4u in forum Sun Java Wireless Toolkit
    Replies: 0
    Last Post: 10-22-2011, 08:32 AM
  2. Changing background color
    By nikkka in forum New To Java
    Replies: 4
    Last Post: 03-12-2011, 05:54 AM
  3. Background color of window
    By Annie George in forum NetBeans
    Replies: 1
    Last Post: 09-18-2010, 09:52 AM
  4. background color with jpanel
    By hannerz06 in forum New To Java
    Replies: 6
    Last Post: 03-31-2010, 03:25 AM
  5. window background color?
    By javan00b in forum New To Java
    Replies: 3
    Last Post: 01-29-2008, 10:43 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts