java - Detecting Mouse Wheel Stops -
i building simple program , need boolean false
when user's mouse wheel becomes stopped after having been scrolling. code below more information.
public class wheelhandler extends mouseadapter { public void mousewheelmoved(mousewheelevent e) { moved = e.getwheelrotation(); scrolling = true; } scrolling = false; // how make run when wheel becomes stopped? }
rolling series of discrete movements, you're going have decide how time between discrete movements means "stopped", , use swing timer check time increment. need decide on arbitrary time slice (below it's int timer_delay = 100
or 100 msec), start timer when user moves mouse wheel, , cancel same timer when use wheel again before slice up.
for example:
import java.awt.dimension; import java.awt.event.actionevent; import java.awt.event.actionlistener; import java.awt.event.mouseadapter; import java.awt.event.mousewheelevent; import javax.swing.*; @suppresswarnings("serial") public class notifywheelstopped extends jpanel { private static final int pref_w = 400; private static final int pref_h = pref_w; private static final string wheel_stopped = "wheel has stopped"; private static final string wheel_moving = "wheel moving"; public static final int timer_delay = 100; private jlabel notificationlabel = new jlabel(wheel_stopped, swingconstants.center); private wheelhandler wheelhandler = new wheelhandler(); private timer wheelmovementtimer; public notifywheelstopped() { add(notificationlabel); addmousewheellistener(wheelhandler); } @override public dimension getpreferredsize() { if (ispreferredsizeset()) { return super.getpreferredsize(); } return new dimension(pref_w, pref_h); } private class wheelhandler extends mouseadapter { @override public void mousewheelmoved(mousewheelevent e) { notificationlabel.settext(wheel_moving); if (wheelmovementtimer != null && wheelmovementtimer.isrunning()) { wheelmovementtimer.stop(); } wheelmovementtimer = new timer(timer_delay, new wheelmovementtimeractionlistener()); wheelmovementtimer.setrepeats(false); wheelmovementtimer.start(); } } private class wheelmovementtimeractionlistener implements actionlistener { @override public void actionperformed(actionevent e) { notificationlabel.settext(wheel_stopped); } } private static void createandshowgui() { notifywheelstopped mainpanel = new notifywheelstopped(); jframe frame = new jframe("notifywheelstopped"); frame.setdefaultcloseoperation(jframe.dispose_on_close); frame.getcontentpane().add(mainpanel); frame.pack(); frame.setlocationbyplatform(true); frame.setvisible(true); } public static void main(string[] args) { swingutilities.invokelater(new runnable() { public void run() { createandshowgui(); } }); } }
even delay of 100 msec, notification can jumpy when wheel turned slowly.
Comments
Post a Comment