Solved

button background

Posted on 2004-10-16
6
193 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Optimize your web performance

What's in the eBook?
- Full list of reasons for poor performance
- Ultimate measures to speed things up
- Primary web monitoring types
- KPIs you should be monitoring in order to increase your ROI

 
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

[Webinar] How Hackers Steal Your Credentials

Do You Know How Hackers Steal Your Credentials? Join us and Skyport Systems to learn how hackers steal your credentials and why Active Directory must be secure to stop them. Thursday, July 13, 2017 10:00 A.M. PDT

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

INTRODUCTION Working with files is a moderately common task in Java.  For most projects hard coding the file names, using parameters in configuration files, or using command-line arguments is sufficient.   However, when your application has vi…
Java contains several comparison operators (e.g., <, <=, >, >=, ==, !=) that allow you to compare primitive values. However, these operators cannot be used to compare the contents of objects. Interface Comparable is used to allow objects of a cl…
Viewers will learn about arithmetic and Boolean expressions in Java and the logical operators used to create Boolean expressions. We will cover the symbols used for arithmetic expressions and define each logical operator and how to use them in Boole…
Viewers will learn about basic arrays, how to declare them, and how to use them. Introduction and definition: Declare an array and cover the syntax of declaring them: Initialize every index in the created array: Example/Features of a basic arr…
Suggested Courses

632 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