java - How to replace click button with hold? -
hi want make button work buy holding set number of seconds instead of clicking on it, button in question opens new jframe,
button.addactionlistener(new actionlistener() { public void actionperformed(actionevent ae) { frame.dispose(); new secondframe(); } });
thanks,
as suggested codebender, can use mouselistener
instead.which have mousepressed()
, mousereleased()
methods.i have calculated time currenttimemillis()
between each successive press/release.
jframe f = new jframe(); jbutton b = new jbutton(); f.add(b); f.pack(); f.setvisible(true); b.addmouselistener(new mouselistener() { long start_time, stop_time; @override public void mouseclicked(mouseevent e) {} @override public void mousepressed(mouseevent e) { start_time = system.currenttimemillis(); system.out.println("" + start_time); } @override public void mousereleased(mouseevent e) { stop_time = system.currenttimemillis(); system.out.println("" + stop_time); if (stop_time - start_time > 1000) { new jframe().setvisible(true); } } @override public void mouseentered(mouseevent e) {} @override public void mouseexited(mouseevent e) {} });
you can increase or decrease given time.also @ mouseadapter.
Comments
Post a Comment