Search code examples
colorsjbuttonpressed

I don't want to change color of JButton when pressed


Color newColor = new Color(197,222,90);
JButton newButton;
newButton = new JButton(icon);
newButton.setBacgroundColor(newColor);

When it is pressed it changes color. How can I keep it from changing color? I have multiple buttons, so if there is solution in one or two rows please help me, and keep in mind that I'm beginner, writing some huge classes won't help me, because I have multiple buttons with different names to be affected with this.

EDIT: Solution in one line is:

 UIManager.put("Button.select", newColor);

But it changes all button colors but I need another to have different a color.

EDIT2: After some research I figured out there isn't an easy solution (but it should be). How I see it I have 2 solutions, 1. is to break buttons to separate classes and set UIManager for them, and second is to make custom buttons. It is just too much work for button.


Solution

  • I've found nothing that can change that particular behavior on a normal JButton. The problem being, that whatever you write in your actionlistener for the button, will occur AFTER you've let go of the mousebutton, and not "while clicking".

    There are workarounds, however.

    My preferred choice is, to remove all graphics from the button, and then add your own images to the button's regular and pressed states. You could take a screenshot of your GUI, cut out the button, and set that image to be both states.

    JButton myButton = new JButton();
    
    // Sets button x, y, width, height. Make the size match the image.
    myButton.setBounds(5, 30, 100, 30);
    
    // Remove border-graphics.
    myButton.setBorder(null);
    
    // Remove default graphics from the button
    myButton.setContentAreaFilled(false);
    
    // Remove the focus-indicating dotted square when focused (optional)
    myButton.setFocusPainted(false);
    
    // Here, myImage is a simple BufferedImage object.
    // You can set one like this, provided you have an "images" package,
    // next to your main class (ex: com.somecompany.someprogram.images),
    // that contains an image:
    
    BufferedImage myImage = ImageIO.read(getClass().getResource("images/myImage.png"));
    
    // Then we simply apply our image to both states for the button, and we're done.
    myButton.setIcon(new ImageIcon(myImage));
    
    myButton.setPressedIcon(new ImageIcon(myImage));
    

    Obviously there are many ways to retain and load an image, but since that's not the issue here, I'll leave additional methods out of it.

    There's no need to go through it all countless times, though. It should be pretty easy to write your own custom implementation of the JButton class, in which a custom constructor takes a single parameter, being the BufferedImage, and then the constructor sets it up accordingly (changes the icons). Then all you have to do when you create a new JButton, is to use your own class, and pass it an image:

    JButton btn = new MyCustomJButton(myImage);
    

    You could also easily get along with very few images. All you need is a HashMap which holds all the images, with a String as a key. Imagine you need 4 OK-buttons. You make a single image of a button with the text "OK" written on it. Then you put that image into the HashMap, like so:

    myMap.put("OK", myImage);
    

    Then you could do this when creating a button, over and over again if you'd like more:

    JButton btn = new MyCustomJButton(myMap.get("OK"));
    

    Alternatively: Another way of achieving this, which is pretty elaborate, but probably considered "the right way", is to use ButtonUI, as presented in this answer to another post.