|
Replies:
8
-
Last Post:
Nov 12, 2008 2:32 PM
by: jaded83
|
Threads:
[
Previous
|
Next
]
|
|
|
|
|
|
Basic JButton Background color issue
Posted:
Nov 11, 2008 5:36 AM
|
|
|
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
|
|
|
|
|
|
|
Re: Basic JButton Background color issue
Posted:
Nov 11, 2008 7:08 AM
in response to: ecrouse
|
|
|
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. . .
|
|
|
|
|
|
|
|
Re: Basic JButton Background color issue
Posted:
Nov 12, 2008 9:55 AM
in response to: evildeathmath
|
|
|
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.
|
|
|
|
|
|
|
|
Re: Basic JButton Background color issue
Posted:
Nov 11, 2008 7:35 AM
in response to: ecrouse
|
|
|
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.
|
|
|
|
|
|
|
|
Re: Basic JButton Background color issue
Posted:
Nov 11, 2008 3:24 PM
in response to: walterln
|
|
|
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...
|
|
|
|
|
|
|
|
Re: Basic JButton Background color issue
Posted:
Nov 12, 2008 2:16 AM
in response to: ecrouse
|
|
|
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.
|
|
|
|
|
|
|
|
Re: Basic JButton Background color issue
Posted:
Nov 12, 2008 4:26 AM
in response to: walterln
|
|
|
Where can I find a list of all key values like the one you used "Button.select". Looked everywhere and can't find!
|
|
|
|
|
|
|
|
Re: Basic JButton Background color issue
Posted:
Nov 12, 2008 8:18 AM
in response to: ecrouse
|
|
|
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.
|
|
|
|
|