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
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
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
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().setL ayout(bord erLayout1) ;
JPanel panel1 = new JPanel();
panel1.setLayout(borderLay out1);
for (int i = 0; i < 10; i++) {
buttons[i] = new JButton("Hello" + i);
buttons[i].addActionListen er(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(ActionEven t e) {
for (int i = 0; i < 10; i++) {
if (e.getSource() == buttons[i]) {
buttons[i].setBackground(C olor.YELLO W);
}
else {
buttons[i].setBackground(C olor.WHITE );
}
}
}
}
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().setL
JPanel panel1 = new JPanel();
panel1.setLayout(borderLay
for (int i = 0; i < 10; i++) {
buttons[i] = new JButton("Hello" + i);
buttons[i].addActionListen
panel1.add(buttons[i]);
}
this.getContentPane().add(
}
public static void main(String[] args) {
ButtonTest buttonTest = new ButtonTest();
buttonTest.setSize(500, 500);
buttonTest.show();
}
/**
* actionPerformed
*
* @param e ActionEvent
*/
public void actionPerformed(ActionEven
for (int i = 0; i < 10; i++) {
if (e.getSource() == buttons[i]) {
buttons[i].setBackground(C
}
else {
buttons[i].setBackground(C
}
}
}
}
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().setL ayout(bord erLayout1) ;
JPanel panel1 = new JPanel();
panel1.setLayout(borderLay out1);
for (int i = 0; i < 10; i++) {
buttons[i] = new JButton("Hello" + i);
buttons[i].addActionListen er(this);
buttons[i].setBackground(C olor.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(ActionEven t e) {
for (int i = 0; i < 10; i++) {
if (e.getSource() == buttons[i]) {
if (oldbutton != null) {
oldbutton.setBackground(Co lor.WHITE) ;
}
buttons[i].setBackground(C olor.YELLO W);
oldbutton = buttons[i];
break;
}
}
}
}
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().setL
JPanel panel1 = new JPanel();
panel1.setLayout(borderLay
for (int i = 0; i < 10; i++) {
buttons[i] = new JButton("Hello" + i);
buttons[i].addActionListen
buttons[i].setBackground(C
panel1.add(buttons[i]);
}
this.getContentPane().add(
}
public static void main(String[] args) {
ButtonTest buttonTest = new ButtonTest();
buttonTest.setSize(500, 500);
buttonTest.show();
}
/**
* actionPerformed
*
* @param e ActionEvent
*/
public void actionPerformed(ActionEven
for (int i = 0; i < 10; i++) {
if (e.getSource() == buttons[i]) {
if (oldbutton != null) {
oldbutton.setBackground(Co
}
buttons[i].setBackground(C
oldbutton = buttons[i];
break;
}
}
}
}
ASKER
thanks
thanks for accepting
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