Why does JButton background color change when clicking ?

Hello,

I have defined :

JButton mybutton = new JButton(Icon xxx) ;

with no associated text and I set both foreground and background colors to black, and an empty border.

This is because the background color of the container which contents this button is also black and I want to see only the button icon.

When I click on the icon , the button background color becomes grey.How can I change this to make it remains black ?

Thanks a lot.

Gege

[500 byte] By [martinelligegea] at [2007-10-3 2:55:46]
# 1

Well, the color changes because that is how the look and feel is defined,

presumably to give better feedback to the user that she has clicked the

button.

If you don't want this, you could try using a different L&F which doesn't do this.

Alternatively, If you've only got the one button, you could just do

UIManager.put( "Button.select", Color.BLACK );

If you have multiple buttons, and you want the other buttons to behave

normally, you can extend the one button and have it change the color

while it is pressed, e.g.,

import java.awt.*;

import java.io.IOException;

import java.net.URL;

import javax.swing.*;

public class ButtonSelect extends JFrame {

public ButtonSelect() throws IOException {

super();

setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

JPanel panel = new JPanel();

panel.add( new JButton( "Normal" ) );

Image img = Toolkit.getDefaultToolkit().getImage( new URL("http://www.google.com/intl/en/images/logo.gif" ) );

ImageIcon ii = new ImageIcon( img );

JButton b = new JButton( ii ) {

static final private String KEY = "Button.select";

public void paint( Graphics g ) {

Color save = null;

if ( getModel().isArmed() && getModel().isPressed() ) {

save = UIManager.getColor( KEY );

UIManager.put( KEY, Color.WHITE );

}

super.paint( g );

if ( save != null )

UIManager.put( KEY, save );

}

};

b.setForeground( Color.WHITE );

b.setBackground( Color.WHITE );

b.setFocusPainted( false );

panel.add( b );

getContentPane().add( panel );

pack();

setVisible( true );

}

public static void main( String[] a ) throws IOException {

new ButtonSelect();

}

}

JayDSa at 2007-7-14 20:44:53 > top of Java-index,Desktop,Core GUI APIs...
# 2
button.setContentAreaFilled(false);
camickra at 2007-7-14 20:44:53 > top of Java-index,Desktop,Core GUI APIs...
# 3

Camickr, when I first saw your post, I went, "Wow, that is way better than my

suggestion", so I had to go try it.

The problem is that it ignores the foreground/background colors, so now

there's the area around the icon that isn't colored, and if the icon had some

transparency, that might be an issue too, as that would show through under

the icon. At a minimum, I think you'd also need to add

button.setMargin( new Insets( 0 , 0, 0 , 0 ) );

However, even with that there seems to be an area just in from the button

bevel that isn't quite right, and it seems right when the foreground and

background aren't ignored. It must be part of the button border because it

doesn't show up when I did

button.setBorderPainted( false );

but then I had no bevel entirely. It doesn't show up when I don't call

button.setContentAreaFilled( false );

either. Would you try this with my example program to see what I am

talking about? What is that thin area?

JayDSa at 2007-7-14 20:44:53 > top of Java-index,Desktop,Core GUI APIs...
# 4

> At a minimum, I think you'd also need to add

button.setMargin( new Insets( 0 , 0, 0 , 0 ) );

Yes, definitely.

> and if the icon had some transparency, that might be an issue too,

It probably would be. You could get around this by adding the button to a JPanel and then add the panel to the GUI.

> What is that thin area?

That thin area is a white line that is part of the button border. It even exists on the text button. I did a print screen of the frame and dropped the image into Paint and you can plainly see the white line as part of the border. You can't control the color of this line it is always white. Therefore your solution only happens to work because your image also has a white background. I used an image with a blue background and you can plainly see the thin white line.

Anyway, the OP has a couple of ideas.

camickra at 2007-7-14 20:44:53 > top of Java-index,Desktop,Core GUI APIs...
# 5
Thanks a lot to both of you JayDS and Camickr. By setting the "Button Select" to Color.black solved my problem.Many thanks again.Gege
martinelligegea at 2007-7-14 20:44:53 > top of Java-index,Desktop,Core GUI APIs...
# 6
Hello Jay,I tried your sample, but when I click on the google icon, the button background becomes dark grey . Isn't supposed to remain white ? Thanks .Gege
martinelligegea at 2007-7-14 20:44:53 > top of Java-index,Desktop,Core GUI APIs...
# 7
another option is to disable the button, and change the actionListener to a mouseListener
Michael_Dunna at 2007-7-14 20:44:53 > top of Java-index,Desktop,Core GUI APIs...
# 8
Thanks for the followup, camickr.Gege, yes, it should remain white, and that's what happens for me.
JayDSa at 2007-7-14 20:44:53 > top of Java-index,Desktop,Core GUI APIs...
# 9

I can across a similar problem, when i create a paintbrush program to select the pen color. I'm not sure why it didn't work when i tried JayDS's code for overriding the JButton Paint.

I solved my way by drawing the graphics on the icon:

BufferedImage image = new BufferedImage(buttonsize,

buttonsize,BufferedImage.TYPE_INT_ARGB);

Graphics2D g2D = image.createGraphics();

g2D.setColor(PanelPixel.colorMap.get(i));

g2D.fillRect(0, 0, buttonsize, buttonsize);

color[i] = new JToggleButton(new ImageIcon(image));

toolbar.add(color[i]);

colorGroup.add(color[i]);

color[i].setSize(buttonsize,buttonsize);

color[i].setPreferredSize(color[i].getSize());

color[i].setMinimumSize(color[i].getSize());

color[i].setMaximumSize(color[i].getSize());

j4dya at 2007-7-14 20:44:53 > top of Java-index,Desktop,Core GUI APIs...