How to set a background color of a JButton in Java?

0 votes
asked Jan 18, 2010 by yatendra-goel

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)?

6 Answers

0 votes
answered Jan 18, 2010 by marionmaiden

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.

0 votes
answered Jan 18, 2010 by zappi

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/

0 votes
answered Jan 18, 2010 by carlos-heuberger
  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);
    ...
    
0 votes
answered Jan 18, 2010 by trashgod

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]
0 votes
answered Jan 17, 2012 by stegeta

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

0 votes
answered Jan 14, 2015 by rahul

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

Welcome to Q&A, where you can ask questions and receive answers from other members of the community.
Website Online Counter

...