Solved

button background

Posted on 2004-10-16
6
185 Views
Last Modified: 2010-03-31
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
0
Comment
Question by:Samooramad
  • 4
6 Comments
 

Expert Comment

by:KaiNikulainen
ID: 12329138
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
0
 
LVL 14

Accepted Solution

by:
sudhakar_koundinya earned 250 total points
ID: 12329181
are u expecting like this??

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 {
  BorderLayout borderLayout1 = new BorderLayout();
  JButton button1 = new JButton("Hello");
  JButton button2 = new JButton("Hello1");
  JButton button3 = new JButton("Hello2");

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

  void jbInit() throws Exception {
    this.getContentPane().setLayout(borderLayout1);
    JPanel panel1 = new JPanel();
    panel1.add(button1);
    JPanel panel2 = new JPanel();
    panel2.add(button2);
    JPanel panel3 = new JPanel();
    panel3.add(button3);
    this.getContentPane().add(panel1,borderLayout1.EAST);
    this.getContentPane().add(panel2,borderLayout1.CENTER);
    this.getContentPane().add(panel3,borderLayout1.WEST);
    button1.addActionListener(this);
    button2.addActionListener(this);
    button3.addActionListener(this);
  }

  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) {
    if (e.getSource() == button1) {
      button1.setBackground(Color.YELLOW);
      button2.setBackground(Color.BLACK);
      button3.setBackground(Color.BLACK);
    }
    else if (e.getSource() == button2) {
      button2.setBackground(Color.YELLOW);
      button1.setBackground(Color.BLACK);
      button3.setBackground(Color.BLACK);

    }
    else if (e.getSource() == button3) {
      button3.setBackground(Color.YELLOW);
      button2.setBackground(Color.BLACK);
      button1.setBackground(Color.BLACK);

    }

  }

}
0
 
LVL 14

Expert Comment

by:sudhakar_koundinya
ID: 12329210
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);
      }
    }

  }

}
0
Maximize Your Threat Intelligence Reporting

Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

 
LVL 14

Expert Comment

by:sudhakar_koundinya
ID: 12329245
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;
      }

    }

  }

}
0
 

Author Comment

by:Samooramad
ID: 12329867
thanks
0
 
LVL 14

Expert Comment

by:sudhakar_koundinya
ID: 12329897
thanks for accepting
0

Featured Post

Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
mergeTwo  challenge 13 72
bunnyEars2 challenge 6 70
countPairs challenge 7 58
java 8 lambda expresssions exception handling 3 41
Introduction This article is the first of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article explains our test automation goals. Then rationale is given for the tools we use to a…
Introduction This article is the second of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers the basic installation and configuration of the test automation tools used by…
Viewers learn about the “for” loop and how it works in Java. By comparing it to the while loop learned before, viewers can make the transition easily. You will learn about the formatting of the for loop as we write a program that prints even numbers…
Viewers will learn about if statements in Java and their use The if statement: The condition required to create an if statement: Variations of if statements: An example using if statements:

762 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now