java - Using Swing Timer to delay task -
i using swing timer delay task specific period of time. time interval decided user.
in gui, have spinnerdatemodel accept time @ task has performed.
spinnerdatemodel date = new spinnerdatemodel(); jspinner spinner = new jspinner(date); frame.getcontentpane().add(spinner); date futuredate = date.getdate();
now, timer has arguments timer(int delay, actionlistener task)
actionlistener task = new actionlistener(){ @override public void actionperformed(actionevent arg0) { //send function } }; timer timer = new timer(delay, task); timer.setrepeats(false); timer.start();
how set delay time specified user?
with checking prevent negative delay, like:
delay=math.max(0,futuredate.gettime()-system.currenttimemillis()); delay=math.min(delay,integer.max_value); // or: // if(delay>integer.max_value) { throw new exception-of-some-sort } timer timer=new timer((int)delay,task);
should trick.
this calculate delay based on number of milliseconds until (presumed future) date selected user.
Comments
Post a Comment