Results 1 to 2 of 2
Thread: button background color
- 02-11-2012, 04:04 AM #1
Member
- Join Date
- Aug 2009
- Posts
- 60
- Rep Power
- 0
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); } }
- 02-11-2012, 05:26 AM #2
Senior Member
- Join Date
- Jul 2009
- Posts
- 1,143
- Rep Power
- 5
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
-
How to change background color
By Jeffrey4u in forum Sun Java Wireless ToolkitReplies: 0Last Post: 10-22-2011, 08:32 AM -
Changing background color
By nikkka in forum New To JavaReplies: 4Last Post: 03-12-2011, 05:54 AM -
Background color of window
By Annie George in forum NetBeansReplies: 1Last Post: 09-18-2010, 09:52 AM -
background color with jpanel
By hannerz06 in forum New To JavaReplies: 6Last Post: 03-31-2010, 03:25 AM -
window background color?
By javan00b in forum New To JavaReplies: 3Last Post: 01-29-2008, 10:43 AM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks