Solved

button background

Posted on 2004-10-16
6
188 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
Live: Real-Time Solutions, Start Here

Receive instant 1:1 support from technology experts, using our real-time conversation and whiteboard interface. Your first 5 minutes are always free.

 
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

Gigs: Get Your Project Delivered by an Expert

Select from freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely and get projects done right.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Protect jar file - windows app 2 50
firstswap challenge 20 77
GUI builder for Eclipse? 8 35
java jdbc batch example 8 30
For customizing the look of your lightweight component and making it look lucid like it was made of glass. Or: how to make your component more Apple-ish ;) This tip assumes your component to be of rectangular shape and completely opaque. (COD…
Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
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 how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …

813 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

12 Experts available now in Live!

Get 1:1 Help Now