The Source for Java Technology Collaboration

Home » java.net Forums » Java Desktop Technologies » Swing & AWT

Thread: Basic JButton Background color issue

Welcome, Guest Help
Login Login
Guest Settings Guest Settings
This question is not answered. Helpful answers available: 2. Correct answers available: 1.

Reply to this Thread Reply to this Thread Search Forum Search Forum Back to Thread List Back to Thread List

Permlink Replies: 8 - Last Post: Nov 12, 2008 2:32 PM by: jaded83 Threads: [ Previous | Next ]
ecrouse

Posts: 19
Basic JButton Background color issue
Posted: Nov 11, 2008 5:36 AM
 
  Click to reply to this thread Reply

I would like to change the normal color of the depressed state (while user clicks on button and holds button down) from the standard metal background color to a different color. When the user releases the button I would like the button to resume it's normal background color.

I tried overriding the processMouseEvents() function by extend a MyButton class from JButton. I caught a MOUSE_PRESSED event and setBackGround( SomeColor ) this worked, however, I lost state changing behavior. But when I tried inserting a super.processMouseEvent() function in my overriden function I gained the normal state changing behavior but the background no longer changed during the depressed state like I had before.

Thanks a Million,

-Erick

evildeathmath

Posts: 1
Re: Basic JButton Background color issue
Posted: Nov 11, 2008 7:08 AM   in response to: ecrouse
 
  Click to reply to this thread Reply

Use an ActionListener instead of overriding processMountEvents(), as below:

final JButton button = new JButton ("foo");
final Color normal_background = button.getBackground ();
buttons.addActionListener (new ActionListener () {
public void actionPerformed (ActionEvent e) {
Color current = button.getBackground ();
if (current.equals (normal_background))
button.setBackground (pressed_background_color);
else
button.setBackground (normal_background);
}
});

Hope this helps. . .

lafery

Posts: 2
Re: Basic JButton Background color issue
Posted: Nov 12, 2008 9:55 AM   in response to: evildeathmath
 
  Click to reply to this thread Reply

The action listener will not work in this instance because it will call actionPerformed after the mouse is released and thus, after the button is depressed.

walterln

Posts: 278
Re: Basic JButton Background color issue
Posted: Nov 11, 2008 7:35 AM   in response to: ecrouse
 
  Click to reply to this thread Reply

For just the Metal L&F you can use the following:
UIManager.getDefaults().put("Button.select", Color.RED);

This changes it for all buttons though.

ecrouse

Posts: 19
Re: Basic JButton Background color issue
Posted: Nov 11, 2008 3:24 PM   in response to: walterln
 
  Click to reply to this thread Reply

I tried using the ActionListener method and to my surprise it still didn't work. It sets the background color but not until after I release the button, not during the button press. I looked into the UIManager and discovered that everything I need to do pertaining to the look was in there. There is an alternative to the getDefaults() mentioned which gives you much more flexibility and control. I came across a class called SynthLookandFeel where I can set the states of the button in an xml file which is pretty nifty. I'm so glad you led me down this path because I was racking my brains and I knew there had to be an easier way. Thank you both for your help...

walterln

Posts: 278
Re: Basic JButton Background color issue
Posted: Nov 12, 2008 2:16 AM   in response to: ecrouse
 
  Click to reply to this thread Reply

Yup, the L&F is the place to look for such changes.

If you want to do it for a single button though, you need to add change listener to the button model and check its isPressed() when the state changes.

But I think Synth (or Nimbus) also allows you to set L&F properties per component, but not sure how to do that.

ecrouse

Posts: 19
Re: Basic JButton Background color issue
Posted: Nov 12, 2008 4:26 AM   in response to: walterln
 
  Click to reply to this thread Reply

Where can I find a list of all key values like the one you used "Button.select". Looked everywhere and can't find!

walterln

Posts: 278
Re: Basic JButton Background color issue
Posted: Nov 12, 2008 8:18 AM   in response to: ecrouse
 
  Click to reply to this thread Reply

Button.select I found by looking at Basic- or MetalButtonUI source code.

Also I use this utility program (forgot where I originally got it) to view all defaults a L&F installs.

jaded83

Posts: 41
Re: Basic JButton Background color issue
Posted: Nov 12, 2008 2:32 PM   in response to: ecrouse
 
  Click to reply to this thread Reply

LAF color keys (good source) http://home.tiscali.nl/~bmc88/java/sbook/061.html




 XML java.net RSS