简体   繁体   中英

is there a NO MOTION mouse listener?

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("");
   }
}

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM