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
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]
just use null
to use the default color:
button1.setBackground(Color.ORANGE);
button2.setBackground(null);
...
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);
...
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
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.
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
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/