Gigatron
Gigatron

Reputation: 2055

How to draw a colored rectangle on a JButton?

I'm going to enhance an application with a Swing UI, to allow the user to pick colors so they're not stuck with the default color choices.

It is common for other applications to have shaded rectangles drawn on each button that activates a color selector, with the rectangle's color changing accordingly when a new color is selected. I am trying to achieve the same effect by placing a small JPanel with the selected color on the button, but that results in a tiny square in the middle of the button, instead of filling most of the surface of the button.

I figure another way would be to dynamically generate rectangular icons with the colors and then add the appropriate icon to each button, but surely there must be a simpler way?

Upvotes: 2

Views: 2895

Answers (3)

mKorbel
mKorbel

Reputation: 109815

put there JButton.setIcon with expected Rectangle, for example

EDIT

I am trying to achieve the same effect by placing a small JPanel with the selected color on the button, but that results in a tiny square in the middle of the button, instead of filling most of the surface of the button.

only JFrame (BorderLayout) and JPanel (FlowLayout) have got pre-implemented LayoutManager, for rest of JComponents (add one JComponent to the another JComponent) you have to define LayoutManager, please read this thread

Upvotes: 3

Guillaume Polet
Guillaume Polet

Reputation: 47608

Here is an example using setBackground that works for me:

import java.awt.Color;
import java.awt.Dimension;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

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

public class Test {

    public static void main(String... args) {
        JFrame frame = new JFrame();
        JPanel panel = new JPanel();
        final JButton button = new JButton("Hello");
        button.setOpaque(true);
        panel.add(button);
        button.setBackground(Color.RED);
        button.setOpaque(true);
        button.addActionListener(new ActionListener() {

            @Override
            public void actionPerformed(ActionEvent e) {
                Color c = JColorChooser.showDialog(button, "Choose a color", button.getBackground());
                if (c != null) {
                    button.setBackground(c);
                }
            }
        });
        frame.setContentPane(panel);
        frame.setPreferredSize(new Dimension(800, 600));
        frame.pack();
        frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
        frame.setVisible(true);
    }
}

Upvotes: 2

Maarten Blokker
Maarten Blokker

Reputation: 604

Every swing component is drawn using the JComponent.drawComponent(...) method, you can override the default behavior if you want. For example, to make a panel like you suggest. It is worth noting though, that you can change a jpanels background color to achieve the exact same thing.

    JColorChooser chooser = new JColorChooser(Color.BLACK);
    chooser.setVisible(true);

    Color color = chooser.getColor();
    if (color!=null) {
        colorPanel.setBackground(color);
    }

Where colorPanel would be your JPanel indicating your color.

Upvotes: 1

Related Questions