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
:
Unfortunately, the concept of "background color" is different when using the Windows LAF; the background color is the color drawn around the button:
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?
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!
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.
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."
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.
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With