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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With