5

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.

Chris K
  • 1,703
  • 1
  • 14
  • 26

1 Answers1

1

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

Community
  • 1
  • 1
Vovka
  • 599
  • 3
  • 10
  • That is pretty close to what I want (the color shows up correctly) but I get no visual indication of clicks when using `WindowsLookAndFeel` (I do when using the default look and feel). – Chris K Sep 29 '15 at 19:02
  • Awarded the bounty since I did not add the visual indication stipulations until after I started the bounty. – Chris K Oct 06 '15 at 15:53
  • Sry, have no time for research and test now. Here some starting points: 0) http://bugs.java.com/bugdatabase/view_bug.do?bug_id=4880747; 1) create you own ButtonUI http://stackoverflow.com/questions/5751311/creating-a-custom-button-in-java-with-jbutton/5755124#5755124 ; 2) http://stackoverflow.com/questions/15043218/jbutton-background-images?rq=1 ; 3) use images as background http://stackoverflow.com/a/11931487/5379032 4) WindowsXP - your code works correctly, no problem. Win8 - problem. Win7 not tested. – Vovka Oct 07 '15 at 12:48