Question

I am using netbeans platform module to develop this desktop application. I am using drag and drop facility in netbeans in the developement.

I needed to create a toolbar which is having few buttons..I need to create a gradient color for these buttons.

I dragged and dropped JToolBar, over it I dragged and placed JButton objects. In the properties of the button I have selected a color for which I want a shaded color. In the custom code I have modified.

jbutton = new javax.swing.Jbutton();

as below

jbutton = new javax.swing.JButton(){
    @Override
    protected void paintComponent(Graphics grphcs) {
        Graphics2D g2d = (Graphics2D) grphcs;
        g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
           RenderingHints.VALUE_ANTIALIAS_ON);
        GradientPaint gp = new GradientPaint(0, 0,
            getBackground().brighter().brighter().brighter(), 0, getHeight(),
            getBackground().darker());
         g2d.setPaint(gp);
        g2d.fillRect(0, 0, getWidth(), getHeight());
        super.paintComponent(grphcs);
    }};

When I used the above code for a JPanel in my project it worked but it is not showing any effect when I used it for a button.

How to get a gradient color for a button placed in a toolbar?

Was it helpful?

Solution

The button has a contentAreaFilled attribute which determines if the look and feel should paint the content area of the button. When you call super.paintComponent, the look and feel delegate will paint over what you have done.

You can set this property to false and it should then work.

For example...

RoundButton

import java.awt.Color;
import java.awt.Dimension;
import java.awt.EventQueue;
import java.awt.GradientPaint;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.GridBagLayout;
import java.awt.RenderingHints;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;

public class GradientButtons {

    public static void main(String[] args) {
        new GradientButtons();
    }

    public GradientButtons() {
        EventQueue.invokeLater(new Runnable() {
            @Override
            public void run() {
                try {
                    UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
                } catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
                }

                JFrame frame = new JFrame("Testing");
                frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                frame.setLayout(new GridBagLayout());
                frame.add(new RoundButton("Click me"));
                frame.pack();
                frame.setLocationRelativeTo(null);
                frame.setVisible(true);
            }
        });
    }

    public class RoundButton extends JButton {

        public RoundButton(String text) {
            super(text);
            setBorderPainted(false);
            setContentAreaFilled(false);
            setFocusPainted(false);
            setOpaque(false);
        }

        @Override
        public Dimension getPreferredSize() {
            Dimension size = super.getPreferredSize();
            int radius = Math.max(size.width, size.height);
            size.width = radius;
            size.height = radius;
            return size;
        }

        @Override
        protected void paintComponent(Graphics g) {
            Graphics2D g2d = (Graphics2D) g;
            g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
                    RenderingHints.VALUE_ANTIALIAS_ON);
            GradientPaint gp = new GradientPaint(0, 0,
                    Color.RED, 0, getHeight(),
                    Color.YELLOW);
            g2d.setPaint(gp);
            g2d.fillOval(0, 0, getWidth(), getHeight());
            super.paintComponent(g);
        }

    }

}

You may want to check the ButtonModel's armed and/or pressed state so you can also change the way that the button is painted when clicked, as a suggestion...

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top