views:

553

answers:

4

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

+1  A: 

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.

marionmaiden
+3  A: 

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/

Zappi
+1 for nice JWS link.
trashgod
+3  A: 
  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);
    ...
    
Carlos Heuberger
+1 for JToggleButton. Is using `null` reliable?
trashgod
+1 for the suggesting the best approach (using JToggleButton). I was looking for this kind of approach. Thanks
Yatendra Goel
@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 icons
Carlos Heuberger
A: 

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]
trashgod