1

I have 5 JButtons: b1, b2, b3, b4, b5. By default, their color is gray. When I click on any button, the background of that button changes to white. When I click another button, I want that previous clicked button to change its background to gray, and this newly clicked button to change its background to white. Here is the code that I wrote:

int liveButton = 0; //holds the value of the button that is last clicked.
//0 indicates no button clicked (in the beginning)

private void ChangeInUsersList(int clickedButton) {
    switch(liveButton) {
        case 1 : b1.setBackground(Color.GRAY);
                 break;
        case 2 : b2.setBackground(Color.GRAY);
                 break;
        case 3 : b3.setBackground(Color.GRAY);
                 break;
        case 4 : b4.setBackground(Color.GRAY);
                 break;
        case 5 : b5.setBackground(Color.GRAY);
                 break;
        default: System.out.println("No button to change");
    }
    liveButton = clickedButton;// store the clicked button to change its
    //background later
}
private void b1ActionPerformed(java.awt.event.ActionEvent evt) {
    ChangeInUsersList(1);
    b1.setBackground(new java.awt.Color(255,255,255));
}

private void b2ActionPerformed(java.awt.event.ActionEvent evt) {
    ChangeInUsersList(2);
    b2.setBackground(new java.awt.Color(255,255,255));
}

private void b3ActionPerformed(java.awt.event.ActionEvent evt) {
    ChangeInUsersList(3);
    b3.setBackground(new java.awt.Color(255,255,255));
}

private void b4ActionPerformed(java.awt.event.ActionEvent evt) {
    ChangeInUsersList(4);
    b4.setBackground(new java.awt.Color(255,255,255));
}

private void b5ButtonActionPerformed(java.awt.event.ActionEvent evt) {
    ChangeInUsersList(5);
    b5.setBackground(new java.awt.Color(255,255,255));
}

However, its not working as expected. When i click on a button, its background does change to white. However, if i click on some other button after that, the former button's background doesnt change to grey. I tried replacing Color.GREY with new java.awt.Color(236,233,216) - the rgb for grey but it still doesnt work.

Jan Hrcek
  • 626
  • 11
  • 24
mithun1538
  • 1,447
  • 8
  • 25
  • 32
  • are you trying to simulate what a ButtonGroup does? e.g. only one button can be selected/toggled? btw.: replace new java.awt.Color(255,255,255) with java.awt.Color.white – Tedil Apr 11 '10 at 17:41
  • yes! but i didnt realise that ButtonGroup exists. I assumed that buttongroup was only for radio buttons. – mithun1538 Apr 12 '10 at 20:22

4 Answers4

2

If you ever do need to color a button and then set the color back to its original default state (system gray), use

button.setBackground(null);

This will remove any previous color setting.

(I have an application where I need to click a few buttons, keep track of which ones I clicked, then when I've performed a function, I "unclick" them. I could have used toggle buttons, but this one line change to add this feature was easier than changing my entire component array. Plus, the UI "feel" is right.)

Sasha
  • 21
  • 2
1

Please explain what exactly it is you want to do.

From what you've written I gather you're trying to have only one Button selected at a time. If so replace your JButtons with JToggleButtons and put them in one ButtonGroup. E.g. (pseudo-code):

//[...]
JToggleButton button2 = new JToggleButton(...)
//[...]
ButtonGroup group = new ButtonGroup();
//[...]
group.add(button2);
//[...]

Else if you really wanted to change the buttons' background colors:

private List<JButton> buttons;
private JButton b1, b2, b3, b4, b5;
private void initButtons()
{
   buttons = new ArrayList<JButton>(5); // new List to "save" Buttons in
   buttons.add(b1 = new JButton());
   // etc etc ...
   buttons.add(b5 = new JButton());
}

public void setActiveButton(JButton button)
{
   for(JButton b : buttons)
   {
      b.setBackgroundColor(Color.GREY);
   } 
   button.setBackgroundColor(Color.WHITE);
}

private void b1ActionPerformed(java.awt.event.ActionEvent evt) 
{
   setActiveButton(b1);
   // or to be more "generic"
   // setActiveButton((JButton) evt.getSource());
}
Tedil
  • 1,935
  • 28
  • 32
  • yes, I have only one button selected at a time. What I wanted was that the button that is clicked be highlighted. – mithun1538 Apr 12 '10 at 20:21
0

I fixed it by adding the following line after declaring "liveButton" variable:

Color buttonColor = b1.getBackground();

Later, inside ChangeInUsersList function, I replaced "Color.GRAY" with buttonColor. And it worked :)

mithun1538
  • 1,447
  • 8
  • 25
  • 32
0

You need both setBackground(), setContentAreaFilled(false), setOpaque(true) on your button.

Gereltod
  • 2,043
  • 8
  • 25
  • 39