开发者

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 re开发者_开发问答commendations?

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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜