Link to home
Start Free TrialLog in
Avatar of Samooramad
Samooramad

asked on

button background

hi experts

I have a bunch of buttons. I need code to set the latest (clicked button)'s background color to yellow. when another is clicked .it is turned yellow and the rest go back to white

thanks
Avatar of KaiNikulainen
KaiNikulainen

Hi Samooramad,

    You can keep a reference to the last pressed button in a variable. When any button is pressed, change the color of the button in the variable to white and after that set the clicked button's color to yellow and save a reference to that button into the variable.

Kaitsu
ASKER CERTIFIED SOLUTION
Avatar of sudhakar_koundinya
sudhakar_koundinya

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
this is with array of buttons

package org.prithvi.test;

import javax.swing.*;
import java.awt.*;

/**
 * <p>Title: </p>
 * <p>Description: </p>
 * <p>Copyright: Copyright (c) 2004</p>
 * <p>Company: </p>
 * @author not attributable
 * @version 1.0
 */
import java.awt.event.*;

public class ButtonTest
    extends JFrame
    implements ActionListener {
  FlowLayout borderLayout1 = new FlowLayout();
  JButton buttons[] = new JButton[10];

  public ButtonTest() {
    try {
      jbInit();
    }
    catch (Exception ex) {
      ex.printStackTrace();
    }
  }

  void jbInit() throws Exception {
    this.getContentPane().setLayout(borderLayout1);
    JPanel panel1 = new JPanel();
    panel1.setLayout(borderLayout1);

    for (int i = 0; i < 10; i++) {
      buttons[i] = new JButton("Hello" + i);
      buttons[i].addActionListener(this);
      panel1.add(buttons[i]);
    }

    this.getContentPane().add(panel1);

  }

  public static void main(String[] args) {
    ButtonTest buttonTest = new ButtonTest();
    buttonTest.setSize(500, 500);
    buttonTest.show();
  }

  /**
   * actionPerformed
   *
   * @param e ActionEvent
   */
  public void actionPerformed(ActionEvent e) {
    for (int i = 0; i < 10; i++) {
      if (e.getSource() == buttons[i]) {
        buttons[i].setBackground(Color.YELLOW);
      }
      else {
        buttons[i].setBackground(Color.WHITE);
      }
    }

  }

}
This must be a bit efficient, maintains the last clicked in memory

package org.prithvi.test;

import javax.swing.*;
import java.awt.*;

/**
 * <p>Title: </p>
 * <p>Description: </p>
 * <p>Copyright: Copyright (c) 2004</p>
 * <p>Company: </p>
 * @author not attributable
 * @version 1.0
 */
import java.awt.event.*;

public class ButtonTest
    extends JFrame
    implements ActionListener {
  FlowLayout borderLayout1 = new FlowLayout();
  JButton buttons[] = new JButton[10];
  JButton oldbutton;

  public ButtonTest() {
    try {
      jbInit();
    }
    catch (Exception ex) {
      ex.printStackTrace();
    }
  }

  void jbInit() throws Exception {
    this.getContentPane().setLayout(borderLayout1);
    JPanel panel1 = new JPanel();
    panel1.setLayout(borderLayout1);

    for (int i = 0; i < 10; i++) {
      buttons[i] = new JButton("Hello" + i);
      buttons[i].addActionListener(this);
       buttons[i].setBackground(Color.WHITE);
      panel1.add(buttons[i]);
    }

    this.getContentPane().add(panel1);

  }

  public static void main(String[] args) {
    ButtonTest buttonTest = new ButtonTest();
    buttonTest.setSize(500, 500);
    buttonTest.show();
  }

  /**
   * actionPerformed
   *
   * @param e ActionEvent
   */
  public void actionPerformed(ActionEvent e) {
    for (int i = 0; i < 10; i++) {
      if (e.getSource() == buttons[i]) {
        if (oldbutton != null) {
          oldbutton.setBackground(Color.WHITE);
        }
        buttons[i].setBackground(Color.YELLOW);
        oldbutton = buttons[i];
        break;
      }

    }

  }

}
Avatar of Samooramad

ASKER

thanks
thanks for accepting