• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Ron McLeod
  • Paul Clapham
  • Bear Bibeault
  • Junilu Lacar
Sheriffs:
  • Jeanne Boyarsky
  • Tim Cooke
  • Henry Wong
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • salvin francis
  • Frits Walraven
Bartenders:
  • Scott Selikoff
  • Piet Souris
  • Carey Brown

see this very simple code.

 
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hello guys,
this is a very simple code, but i am confused can you help me out?
in this code, i wanted to changed the color of the button when mouse entered over the button.but problem with the below code
is that when i move mouse over one button both the button color get changes. so, please see this.
thank you.
import java.applet.Applet;
import java.awt.*;
import java.awt.event.*;
public class but extends Applet implements MouseListener
{
Button b1,b2;
public void init()
{
b1=new Button("click1");
b2=new Button("click2");

b1.setBackground(Color.gray);
b2.setBackground(Color.gray);

b1.addMouseListener(this);
b2.addMouseListener(this);
add(b1);
add(b2);
}
public void mousePressed(MouseEvent e)
{}
public void mouseReleased(MouseEvent e)
{}
public void mouseClicked(MouseEvent e)
{}
public void mouseEntered(MouseEvent e)
{
/*
how to change the particular button color which has the mouse over it. at present whenever the mouse come over either of the button, both the color changes.
*/

b1.setBackground(Color.cyan);
b2.setBackground(Color.cyan);

}
public void mouseExited(MouseEvent e)
{

b1.setBackground(Color.gray);
b2.setBackground(Color.gray);

}

}
 
Ranch Hand
Posts: 1492
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Nima,
Make use of the getSource method of the MouseEvent:

Do the same thing for the mouseEntered and mouseExited and you will be done.
Regards,
Manfred.
 
Yeah, but is it art? What do you think tiny ad?
Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop
https://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton
    Bookmark Topic Watch Topic
  • New Topic