5

I have a JFrame that I am putting several JButtons on. Half the JButtons have color coding--i.e. I turn them blue when X event happens--and I use btn.setBackgroundColor(Color). When I use setBackgroundColor, I can see that I look the ones that are normal JButtons have shading/coloring/something that the ones with the setBackgroundColor do not. I've tried making the color transparent to a limited degree, but I still get a flat block of color, rather than a tinted version of the shaded button.

This seems like it should be a pretty easy thing to fix, but it is bugging me right now. I don't want to change the default LAF--it's fine. I don't want to abandon the color change. I do want the buttons to all appear styled (the word I'd use for HTML).

So I'm missing something right here....what is it?

Edited to add:

JFrame frame = new JFrame();
frame.add(new JButton("42"));
JButton btn24 = new JButton("24");
btn24.setBackground(Color.red);
frame.add(btn24);
frame.setVisible(true);

In the above example, "42" will--on my Windows machine--show a slight color variation at the bottom and the top, creating a rounded and shaded effect. The "24" button will show a red square. My question is: Is there a way to make "24" show the rounded/shaded/styled with the red tint on top? Or do I need to simple make all my buttons flat squares for a uniform appearance?

Thanks!

user2363027
  • 121
  • 1
  • 6
  • 1
    Post the code you have tried to do this already and maybe an image of what you mean by "shading/coloring/something that the ones with the setBackgroundColor do not" – Java Devil Jun 24 '13 at 21:04
  • 2
    Look at @mKorbel's answer [here](http://stackoverflow.com/questions/5751311/creating-a-custom-button-in-java-with-jbutton/5755124#5755124). And then up-vote his answer, because it is truly amazing. Also look at camickr's answer [here](http://stackoverflow.com/questions/6256483/how-to-set-the-button-color-of-a-jbutton-not-background-color?rq=1). – Hovercraft Full Of Eels Jun 24 '13 at 21:32
  • I am looking at @mKorbel's answer. camickr's test code showed pretty much the same stuff I was seeing with the setBackgroundColor with Windows and Java 7. – user2363027 Jun 25 '13 at 14:44
  • Sorry...I don't have the rep to upvote. I did poke around some more and am currently looking at the GradientPaint. It's a hassle to implement, but it looks like overriding paintComponent with the gradient paint is what I want. – user2363027 Jun 25 '13 at 16:57
  • Dear Hovercraft and Java Devil, Yes--the problem was that when you set the background color--whether you use the .setBackground(Color) or draw the rectange/shape above it--doesn't (by default) use the gradient. Therefore, in the example above, the 42 button has a gradient (shading/styling) because in the default LAF, it paints the buttons with a gradient. The 24 button (by default on my system at least) shows the color in a plain paint--not with the gradient. Thanks for pointing me in the right direction. If I had the rep, I'd upvote for @mKorbel (and Hovercraft). – user2363027 Jun 26 '13 at 14:35
  • You have a couple of choices. If your JButtons have text, you can change the color of the text. If your JButtons don't have text, you can create BufferedImages with the colors you want, and set the BufferedImages as JButton icons. – Gilbert Le Blanc Nov 27 '16 at 22:50

1 Answers1

3

Create a custom JButton and override the paint method as illustrated bellow :

import static javax.swing.JFrame.EXIT_ON_CLOSE;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.GradientPaint;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Paint;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;

public class Test {

   public static void main(String[] args) {
    JButton btn24 = new DepthButton("24");
    JButton btn25 = new DepthButton("25");
    btn24.setBackground(Color.red);
    btn25.setBackground(Color.GREEN);

    JPanel pane = new JPanel(new BorderLayout());
    pane.add(new JButton("42"), BorderLayout.PAGE_START);

    pane.add(btn24, BorderLayout.PAGE_END);
    pane.add(btn25, BorderLayout.CENTER);

    frame.add(pane);
    frame.pack();
    frame.setDefaultCloseOperation(EXIT_ON_CLOSE);
    frame.setVisible(true);
    }

    /**
    *
    * @author Romain Guy
    */
    public static class DepthButton extends JButton {

        /** Creates a new instance of DepthButton */
        public DepthButton(String text) {
            super(text);
            setContentAreaFilled(false);
        }

        @Override
        protected void paintComponent(Graphics g) {
            Graphics2D g2 = (Graphics2D) g;

            GradientPaint p;
            p = new GradientPaint(0, 0, new Color(0xFFFFFF), 0, getHeight(), getBackground());

            Paint oldPaint = g2.getPaint();
            g2.setPaint(p);
            g2.fillRect(0, 0, getWidth(), getHeight());
            g2.setPaint(oldPaint);

            super.paintComponent(g);
        }
    }
}

And Here is the Result: enter image description here

The example is from an excellent book for advanced java swing : Filthy Rich Clients https://github.com/romainguy/filthy-rich-clients/blob/master/Gradients/TwoStopsGradient/src/DepthButton.java

firephil
  • 830
  • 10
  • 18