Solved

button background

Posted on 2004-10-16
6
189 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
Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

 
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

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
jdbc error in jsp application 20 63
Unhandled exception type Exception 18 42
maven module vs maven project 3 47
CSV file parsing thru Java 13 34
This was posted to the Netbeans forum a Feb, 2010 and I also sent it to Verisign. Who didn't help much in my struggles to get my application signed. ------------------------- Start The idea here is to target your cell phones with the correct…
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…
Viewers learn about the “while” loop and how to utilize it correctly in Java. Additionally, viewers begin exploring how to include conditional statements within a while loop and avoid an endless loop. Define While Loop: Basic Example: Explanatio…
This tutorial explains how to use the VisualVM tool for the Java platform application. This video goes into detail on the Threads, Sampler, and Profiler tabs.

837 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