Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to set the button color of a JButton (not background color)

I have a JButton that I would like to change the background color of to white. When using the Metal Look And Feel, I achieve the desired effect with setBackground:

Metal look-and-feel-styled JButton with a white background

Unfortunately, the concept of "background color" is different when using the Windows LAF; the background color is the color drawn around the button:

Windows look-and-feel-styled JButton with a white background

I would like to use the Windows LAF, but allow the button color of this JButton to be changed to white. How do I do this?

like image 918
Joseph Trebbien Avatar asked Jun 06 '11 18:06

Joseph Trebbien


People also ask

How can I set the color of a JButton?

How can I set the color of a JButton ? but with no success. This just changes the color of button's border. I then tried to override paintComponents Show activity on this post. The best way to color your buttons is to use ImageIcons instead of text. You could use Gimp in order to design them. Make sure that the background is transparent!

How can I change the color of my buttons?

The best way to color your buttons is to use ImageIcons instead of text. You could use Gimp in order to design them. Make sure that the background is transparent! The background color-change after pressing is done by Swing.

What happens if the background color of a button is null?

If set to null, the button became transparent, which is a problem with a differently coloured background. This was on a vanilla windows sun jdk 7. @Cookie : by documentation " [...] The background color is used only if the component is opaque, [...] It is up to the look and feel to honor this property, some may choose to ignore it."

Can only one button have an orange background color?

Now I want that whenever a button is clicked its background color changes to some other color (say orange) to represent that it has been clicked and the background color of all other 3 buttons reset to their default color (in case any of them had Orange background color). So, at one time only one button can have the orange color.


2 Answers

You'll have to decide if it's worth the effort, but you can always create youe own ButtonUI, as shown in this example due to @mKorbel.

like image 192
trashgod Avatar answered Nov 15 '22 00:11

trashgod


I use JDK 6 on XP. It looks like the Window UI doesn't follow the normal painting rules in more ways than 1. As you noticed setBackground() doesn't work. You should be able to do custom painting by telling the component not to fill in the content area:

import java.awt.*;
import javax.swing.*;

public class ButtonBackground extends JFrame
{
    public ButtonBackground()
    {
        setLayout( new FlowLayout() );

        JButton normal = new JButton("Normal");
        add(normal);

        JButton test1 = new JButton("Test 1")
        {
            @Override
            public void paintComponent(Graphics g)
            {
                g.setColor( Color.GREEN );
                g.fillRect(0, 0, getSize().width, getSize().height);
                super.paintComponent(g);
            }
        };
        test1.setContentAreaFilled(false);
        add(test1);
    }

    public static void main(String[] args)
    {
        try
        {
//          UIManager.setLookAndFeel("com.sun.java.swing.plaf.windows.WindowsLookAndFeel");
        }
        catch (Exception e2) {}

        ButtonBackground frame = new ButtonBackground();
        frame.setDefaultCloseOperation( EXIT_ON_CLOSE );
        frame.pack();
        frame.setLocationRelativeTo( null );
        frame.setVisible(true);
    }
}

When you run the code as is it seems to work properly. That is when you click on the button you see the Border change. However is you run with the Windows XP LAF, the Border never changes to you don't see the button click effect.

Therefore, I guess the issue is with the WindowUI and you would need to customize the UI which is probably too complex to do so I don't have a solution.

like image 45
camickr Avatar answered Nov 15 '22 01:11

camickr