0

Is there a way to do both

button.setBackground(Color.green.darker());

and

button.setIcon(new ImageIcon(buttonImg.getScaledInstance(15, 15, Image.SCALE_SMOOTH)));

so that both the image and the background color are visible? Is there some sort of transparency setting on the button's image that I'm missing? Any help would be greatly appreciated.

EDIT:

Here is the image I'm trying to use. As you can see, it has transparency. http://www.clipartlord.com/wp-content/uploads/2013/09/bomb3.png

What I am trying to get is the image to show up on the Button with the background color of green filling in the transparency. Right now when I try to do it, if I add the image at all, the background color is not visible whatsoever. But if I don't add the image, the background color is visible. I hope this helps.

skippy_winks
  • 778
  • 3
  • 14
  • 31
  • Is the image transparent? – MadProgrammer Apr 29 '16 at 05:22
  • And is the button opaque? – RealSkeptic Apr 29 '16 at 05:22
  • And is `setContentAreaFilled` set to `false`? – MadProgrammer Apr 29 '16 at 05:23
  • @MadProgrammer yes the image has transparency around the subject (i.e. no whitespace). What will setContentAreaFilled do? – skippy_winks Apr 29 '16 at 05:25
  • @RealSkeptic yes the button is opaque – skippy_winks Apr 29 '16 at 05:26
  • The area that is "painted" is always the "background", most look and feels "paint" the button shape as a separate element, so the "background" might change, but the actual "button area" won't. Maybe consider adding an image and some code to demonstrate your problem – MadProgrammer Apr 29 '16 at 05:30
  • @MadProgrammer I edited the question with that information. – skippy_winks Apr 29 '16 at 05:34
  • It doesn't appear to be trivial because depending on the look and feel background color doesn't necessarily correspond to color of the button face. eg. With osx default look and feel the face of the button is white regardless. [here](http://stackoverflow.com/questions/5751311/creating-a-custom-button-in-java-with-jbutton/5755124#5755124) is an example of creating a custom button. Do you want to change the background of *all* of your buttons? – matt Apr 29 '16 at 06:32

1 Answers1

3

Probably the LookAndFeel does not support what you want to achieve. Here is an image from the meta theme, on the left, and osx aqua theme.

two different, right is osx aqua and left is metal

If you want all of your JButton backgrounds to change you can try using a LookAndFeel that supports changing the UI. Unfortunately the aqua them does not let you change the default background for a button even using the UIManager#set

If you only want to do one button, you can change the button UI.

button.setUI(new BasicButtonUI());

That one doesn't look too nice, you can set the UI to one from another LookAndFeel,

button.setUI(new MotifButtonUI());

(Also doesn't look very nice, but now it buttons.)

Finally you can also create your own ButtonUI, or do some custom painting.

matt
  • 10,892
  • 3
  • 22
  • 34
  • so the first one, the BasicButtonUI, works almost perfectly. The only issue is that, when it is placed next to other buttons, there is no border or anything to differentiate between them. Is there a way to do this? – skippy_winks Apr 29 '16 at 20:20
  • never mind, I got it. I just created a new Border with a line border and applied that to the buttons, while using BasicButtonUI. Thanks for your help! – skippy_winks Apr 29 '16 at 21:23