Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Change the color of a JButton in JGoodies WindowsLookAndFeel

How do I change the color of a JButton if I am using JGoodies WindowsLookAndFeel? After changing the color the button should still have some visual indication when it is clicked; the color gradient and click animation do not have to be the same as in JGoodies.

Using setBackground() and setForeground() only changes the color of the button outline and the button text:

import com.jgoodies.looks.windows.WindowsLookAndFeel;
...

public class Test {
    public static void main(String[] args) throws UnsupportedLookAndFeelException {
        UIManager.setLookAndFeel(new WindowsLookAndFeel());

        JFrame frame = new JFrame();
        frame.setSize(50, 100);
        JButton button = new JButton("Button");
        button.setBackground(Color.GREEN);
        button.setForeground(Color.RED);
        button.setOpaque(true);
        frame.add(button);
        frame.setVisible(true);
    }
}

enter image description here

I would like to set the color for the whole area of the button not just the outline. (This happens if the WindowsLookAndFeel is not used.)

I have also tried changing the colors in com.jgoodies.looks.windows.WindowsBorders#getButtonBorder() but this does not seem to have any effect.

like image 406
Chris K Avatar asked Nov 09 '22 05:11

Chris K


1 Answers

Try adding call to setContentAreaFilled:

button.setContentAreaFilled(false); //must be before setOpaque
button.setOpaque(true);

or you can override JButton and paint: Change JButton gradient color, but only for one button, not all

like image 119
Vovka Avatar answered Nov 14 '22 21:11

Vovka