Reputation: 1104
I'm working on an alarm application. I'm using SwingUtilities.invokeLater method to call my UI for snooze function. But when I run it I noticed that SwingUtilities.invokeLater function is not being called. Please help me with this problem
public class IsTime {
int hrs;
int min;
int sec;
GregorianCalendar clk=new GregorianCalendar();
Calendar gtl= Calendar.getInstance();
mp3 mix=new mp3();
public void makeReady(int h,int m,int s,String ampm){
Calendar c1=Calendar.getInstance();
c1.set(Calendar.HOUR_OF_DAY,h);
c1.set(Calendar.MINUTE, m);
c1.set(Calendar.SECOND,s);
if("PM".equals(ampm)){
c1.set(Calendar.AM_PM, Calendar.PM);
if(Calendar.getInstance().get(Calendar.AM_PM)==Calendar.PM){
if(c1.after(Calendar.getInstance())){
check(c1);
}
c1.set(Calendar.DAY_OF_YEAR,c1.get(Calendar.DAY_OF_YEAR)+1);
check(c1);
}
}
if(Calendar.getInstance().get(Calendar.AM_PM)==Calendar.AM){
if(c1.after(Calendar.getInstance())){
check(c1);
}
c1.set(Calendar.DAY_OF_YEAR,c1.get(Calendar.DAY_OF_YEAR)+1);
check(c1);
}
if(Calendar.getInstance().get(Calendar.AM_PM)==Calendar.PM){
c1.set(Calendar.DAY_OF_YEAR,c1.get(Calendar.DAY_OF_YEAR)+1);
check(c1);
}
}
public void check(Calendar ch){
while(!(Calendar.getInstance().after(ch))){//this line checks if it is time to sound alarm
}
SwingUtilities.invokeLater(new Runnable(){//here is my invokelater method
public void run(){
System.out.println("git to invoke");
new Snooze();
}
});
mix.start();
}
public void makeReady(int mis){
Calendar sz=Calendar.getInstance();
sz.set(Calendar.SECOND, 0);
sz.set(Calendar.MINUTE, Calendar.getInstance().get(Calendar.MINUTE)+mis);
check(sz);
}
}
Upvotes: 0
Views: 153
Reputation: 47608
This line is really not the proper way to perform "waiting" operation:
while (!Calendar.getInstance().after(ch)) {// this line checks if it is time to sound alarm
}
but rather do:
Thread.sleep(ch.getTimeInMillis()-System.currentTimeMillis());
(and you should make sure it is positive). As it stands now, your code will continously evaluate if it is time which will probably kill your CPU.
For your invokeLater()
, it is hard to tell without a proper fully-working example (an SSCCE). Try to put a log right before the call and see if it pops in your console.
Upvotes: 2