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

    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.

    Thanks!

    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");
    		button.setBackground(buttonUpColor);
        
    		popup = new MouseListener() {
    			public void mouseClicked(MouseEvent e) {
    			}
    
    		     public void mouseEntered(MouseEvent e) {
    		    	 button.setBackground(buttonHighlightColor);
    		     }
    		
    		     public void mouseExited(MouseEvent e) {
    		    	 button.setBackground(buttonUpColor);
    		     }
    		
    		     public void mousePressed(MouseEvent e) {
    		    	 button.setBackground(buttonDownColor);
    		     }
    
    		     public void mouseReleased(MouseEvent e) {
    		    	 button.setBackground(buttonHighlightColor);
    		     }
    		};
    		button.addMouseListener(popup);
    		frame.add(button);
    		frame.setSize(300, 300);
    		frame.setVisible(true);
    
    	}
    }

  2. #2
    camickr is offline Senior Member
    Join Date
    Jul 2009
    Posts
    1,226
    Rep Power
    6

    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");
            button.setOpaque(true);

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
  •