0

I want to change the Java Swing JButton's background color.

The original button looks like this: enter image description here

After I change background color of this button, it looks like this: enter image description here

The orginal one has the "button" look and feeling. However, the yellow one just looks like a yellow plain background with a some text on top.

Question: Is there any easy way that I change the this specific Jbutton's background color but keep the look&feel?

P.S. I know I can make an image and attach to the button. However, the button size will not be always the same so the image may not be a good idea. Is there any other way I can just simply add some codes such as add some border, margin etc so that it has more button-feel?

Update:

I want the button look like a button after I change the background color. It does not matter if I need to add gradient background or do something else. I am looking for a easiest way to do it.

mKorbel
  • 109,525
  • 20
  • 134
  • 319
Joey
  • 2,732
  • 11
  • 43
  • 63
  • What exactly do you want your button to look like? Do you want a gradient background? A border? Something else? – Kevin Workman Jun 04 '14 at 17:03
  • I want the button look like a button after I change the background color. It does not matter if I need to add gradient background or do something else. I am looking for a easiest way to make it look like a button not just a plain colored background with text. – Joey Jun 04 '14 at 17:09
  • What exactly do you mean when you say "look like a button"? What does a button look like, specifically? – Kevin Workman Jun 04 '14 at 17:13

2 Answers2

1

You can have a GradientPaint as your background.

public final class JGradientButtonDemo {
    public static void main(String[] args){
        SwingUtilities.invokeLater(new Runnable(){
            @Override
            public void run() {
                createAndShowGUI();         
            }
        });
    }

    private static void createAndShowGUI(){
        final JFrame frame = new JFrame("Gradient JButton Demo");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.getContentPane().setLayout(new FlowLayout());
        frame.add(JGradientButton.newInstance());
        frame.setSize(new Dimension(300, 150)); // used for demonstration
        //frame.pack();
        frame.setLocationRelativeTo(null);
        frame.setVisible(true);
    }

    private static final class JGradientButton extends JButton{
        private JGradientButton(){
            super("Gradient Button");
            setContentAreaFilled(false);
            setFocusPainted(false); // used for demonstration
        }

        @Override
        protected void paintComponent(Graphics g){
            Graphics2D g2 = (Graphics2D)g.create();
            g2.setPaint(new GradientPaint(
                    new Point(0, 0), 
                    Color.WHITE, 
                    new Point(0, getHeight()), 
                    Color.PINK.darker()));
            g2.fillRect(0, 0, getWidth(), getHeight());
            g2.dispose();

            super.paintComponent(g);
        }

        public static final JGradientButton newInstance(){
            return new JGradientButton();
        }
    }
}    

enter image description here

Example taken from Change JButton gradient color, but only for one button, not all

Community
  • 1
  • 1
An SO User
  • 24,612
  • 35
  • 133
  • 221
0

Use this external api.Simply put the line
UIManager.setLookAndFeel("com.jtattoo.plaf.aluminium.AluminiumLookAndFeel");
as first line in main method

Don Chakkappan
  • 7,397
  • 5
  • 44
  • 59