How to set a background color of a JButton in Java?
Asked Answered
B

6

5

I am developing a Java Desktop Application. In it I have 4 JButtons on a JPanel. 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.

The current approach that I have applied is that I have implemented the following code in the xxxActionPerformed() method of JButton button1

button1.setBackground(Color.Orange);
button2.setBackground(Color.Gray);
button3.setBackground(Color.Gray);
button4.setBackground(Color.Gray);

and similarly for the rest three buttons.

Now in actual, I don't want the backgroud color as Gray (for unclicked button). Instead, I want the default background color so that the backgroud color will adjust itself to the look-and-feel of the GUI according to the end-user's platform's look-and-feel.

Q1. How can I get the default background color?

Q2. Is this the correct approach to do this or Is there any other mechanism through which I can group all the four buttons in a button group so that only one can have the specified property at one time (like radio buttons)?

Barbet answered 18/1, 2010 at 13:42 Comment(0)
S
8
  1. just use null to use the default color:

    button1.setBackground(Color.ORANGE);
    button2.setBackground(null);
    ...
    
  2. consider using JToggleButtons with a ButtonGroup, set the Icon and PressedIcon of the buttons. No need to change the background color.

    button1 = new JToggleButton(new ImageIcon("0.jpg"));
    button1.setSelectedIcon(new ImageIcon("1.jpg"));
    button2 = new JToggleButton(new ImageIcon("0.jpg"));
    button2.setSelectedIcon(new ImageIcon("2.jpg"));
    ...
    ButtonGroup group = new ButtonGroup();
    group.add(button1);
    group.add(button2);
    ...
    
Superimpose answered 18/1, 2010 at 15:44 Comment(4)
+1 for the suggesting the best approach (using JToggleButton). I was looking for this kind of approach. ThanksBarbet
@trashgod I used it plenty of times without problems, but not with JToggleButtons... anyway it is up to the look and feel to honor it or not :-| that's a good reason to use the color of the UIManager, or use the iconsSuperimpose
null didn't work for me. 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.Suh
@Suh : 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."Superimpose
W
8

You can get the standard background color for buttons from the UIManager:

button1.setBackground(UIManager.getColor("Button.background"));

As far as I know, the keys could change in different look & feels. Here is a nice webstart app that shows all available keys:

http://tips4java.wordpress.com/2008/10/09/uimanager-defaults/

Waste answered 18/1, 2010 at 14:26 Comment(0)
S
8
  1. just use null to use the default color:

    button1.setBackground(Color.ORANGE);
    button2.setBackground(null);
    ...
    
  2. consider using JToggleButtons with a ButtonGroup, set the Icon and PressedIcon of the buttons. No need to change the background color.

    button1 = new JToggleButton(new ImageIcon("0.jpg"));
    button1.setSelectedIcon(new ImageIcon("1.jpg"));
    button2 = new JToggleButton(new ImageIcon("0.jpg"));
    button2.setSelectedIcon(new ImageIcon("2.jpg"));
    ...
    ButtonGroup group = new ButtonGroup();
    group.add(button1);
    group.add(button2);
    ...
    
Superimpose answered 18/1, 2010 at 15:44 Comment(4)
+1 for the suggesting the best approach (using JToggleButton). I was looking for this kind of approach. ThanksBarbet
@trashgod I used it plenty of times without problems, but not with JToggleButtons... anyway it is up to the look and feel to honor it or not :-| that's a good reason to use the color of the UIManager, or use the iconsSuperimpose
null didn't work for me. 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.Suh
@Suh : 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."Superimpose
G
1

Q1.: To get the GUI color of the button, just do this

button1.setBackground(Color.Orange);
button2.setBackground(java.awt.SystemColor.control);
button3.setBackground(java.awt.SystemColor.control);
button4.setBackground(java.awt.SystemColor.control);

With this class (java.awt.SystemColor.*), you can get the color of all elements of your user interface.

Q2.: I've never heard about grouping buttons. Then, I can´t answer you this one.

Hope it helps.

Gradatim answered 18/1, 2010 at 13:55 Comment(0)
M
1

Here's a cross-section of "Button.background" based on this example:

*** Metal javax.swing.plaf.metal.MetalLookAndFeel 636 entries
Button.background: javax.swing.plaf.ColorUIResource[r=238,g=238,b=238]

*** Nimbus com.sun.java.swing.plaf.nimbus.NimbusLookAndFeel 1052 entries
Button.background: com.sun.java.swing.plaf.nimbus.NimbusLookAndFeel$NimbusProperty@60961dff

*** CDE/Motif com.sun.java.swing.plaf.motif.MotifLookAndFeel 550 entries
Button.background: javax.swing.plaf.ColorUIResource[r=174,g=178,b=195]

*** Mac OS X com.apple.laf.AquaLookAndFeel 705 entries
Button.background: com.apple.laf.AquaNativeResources$CColorPaintUIResource[r=238,g=238,b=238]
Mulch answered 18/1, 2010 at 15:45 Comment(0)
C
0

If you wish you can redesign your whole button UI

public class buttonUI extends javax.swing.plaf.basic.BasicButtonUI{

buttonUI(JButton b){
    b.setContentAreaFilled(false);
}

@Override
    public void paint(Graphics g,JComponent c){
             Graphics2D g2d = (Graphics2D) g;
             g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
             AbstractButton b = (AbstractButton) c;
             g2d.setFont(font.deriveFont(11f));
             Dimension d = b.getSize();
             FontMetrics fm = g2d.getFontMetrics();
             g2d.setColor(new Color(100,100,100));
             String caption = b.getText();
             int x = (d.width - fm.stringWidth(caption)) / 2;
             int y = (d.height + fm.getAscent()) / 2 - 2;
             g2d.drawString(caption, x, y);        
        }   }

and in your main piece of code use like

jButton1.setUI(new buttonUI(jButton1));

This how I use it .. you can have your own way too

Changchangaris answered 14/5, 2015 at 8:21 Comment(0)
T
-1

have you looked into the decorator pattern in java you pass a button into a method which decorates it depending on whether the button is the current active one, for example if it hovered over.

public Jbutton decorateButton(JButton b, boolean isHoveredOver){
    if(isHoveredOver)
        b.setBackground(getContentPane().getBackground().GREEN);
    return b;
}

you call this method from the MouseEvent or ActionEvent methods and just issue a repaint() after. put all your buttons into an array or vector and loop through it passing each one in to the decorateButton method and give the initail boolean value of false then on event set it to true. This way the initial value is default and the button is then decorated upon action the buttons will appear to be acting as part of a group

Tnt answered 17/5, 2012 at 8:12 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.