Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

is there a NO MOTION mouse listener?

Tags:

java

swing

I have a map applet, and I have a JLabel following the mouse, whenever the mouse goes over a city the JLable displays the the name of the city and population.

I use the mouseMotionListener's MouseMoved method for that, but i want the label to be there only if the mouse stays still for a couple of seconds above the city.

I dont know if its because I been working on this code for days now, but i cant seem to think of a solution for this using the MouseMoved method, i tried using timers but that didnt work for me (mayb i just did it wrong cause my brain is burned out)

so is there a mouse listener for the mouse staying still? or do you have any recommendations?

here is more or less what I got

public void mouseMoved(MouseEvent evt) {
   int x = evt.getX();
   int y = evt.getY();
   boolean aboveCity = false;
   mouseover.setBounds(x+20, y-10, 200, 20); //mouseover is a JLabel

   for (int i=0;i<cityCounter;i++){
      if (city[i].containsPoint(x,y){
         name = city[i].getName();
         population = city[i].getPopulation();
         aboveCity = true;
      }
   }
   if(aboveCity){
      mouseover.setText(name + ", " + population);
   }
   else{
      mouseover.setText("");
   }
}
like image 775
Alex Avatar asked Dec 29 '22 00:12

Alex


1 Answers

Use a Java javax.swing.Timer. Each time the mouse moves, reset the timer. When the timer goes off, the mouse has been "still" for as long as your timer was set for.

like image 96
Will Hartung Avatar answered Jan 12 '23 08:01

Will Hartung