Is there a NO MOTION mouse listener?

I have a map applet, and I have JLabel after the mouse, whenever the mouse moves around the city, JLable displays the name of the city and population.

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

I donโ€™t know if this was due to the fact that I worked on this code for several days, but I canโ€™t think of a solution for this using the MouseMoved method, I tried to use timers, but this did not help me (mayb, I just did it wrong because my brain burned out)

so is there a mouse to listen to the mouse? 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(""); } } 
+4
source share
1 answer

Use Java javax.swing.Timer. Each time the mouse moves, reset the timer. When the timer goes off, the mouse was โ€œmotionlessโ€ until your timer was set.

+10
source

Source: https://habr.com/ru/post/1343266/


All Articles