Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I make the JButtons all look the same when changing background color on some of them and not on others?

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!

like image 646
user2363027 Avatar asked Oct 21 '22 07:10

user2363027


1 Answers

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

like image 195
firephil Avatar answered Oct 31 '22 19:10

firephil