문제

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.

도움이 되었습니까?

해결책 3

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

다른 팁

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

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());
}

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

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top