views:

54

answers:

3

I have a dialog box that is:

JOptionPane.showMessageDialog(null,"Once medicine is given, measure temperature within 5 minutes." ,"Medication" ,JOptionPane.PLAIN_MESSAGE); 

When the user presses 'ok', it goes straight to a Jframe that ask the user to input the temperature using a slider and then pressing a button that takes it to the next set of things.

Anyways, I want to create somekind of inivisble countdown after the user presses 'ok', so after 5 minutes of idleness on the Jframe menu, one warning dialog box should appear on top of the JFrame and says something like "NEED ATTENTION".

This reminds me of actionListener. but it will be invoked by non-physical element, 5 minutes, (not by any click of button).

So Maybe the code should be like:

JOptionPane.showMessageDialog(null,"Once medicine is given, measure temperature within 5 minutes." ,"Medication" ,JOptionPane.PLAIN_MESSAGE); 


temperature_class temp = new temperature_class(); // going to a different class where the the Jframe is coded

    if (time exceeds 5 minutes) { JOptionPane.showMessageDialog(null, "NEED attention", JOptionPane.WARNING_MESSAGE);}
    else { (do nothing) }

Code works:

JOptionPane.showMessageDialog(null,"measure temp" ,"1" ,JOptionPane.PLAIN_MESSAGE); 

int delay = 3000; //milliseconds
 ActionListener taskPerformer = new ActionListener() {
 public void actionPerformed(ActionEvent evt) {

JOptionPane.showMessageDialog(null,"hurry." ,"Bolus Therapy Protocol" ,JOptionPane.PLAIN_MESSAGE); } };
new Timer(delay, taskPerformer).start();

temperature_class temp = new temperature_class();

However, I want it do it only once. So how do I invoke set.Repeats(false)?

+2  A: 

You could use a TimerTask with a Timer:

class PopTask extends TimerTask {
  public void run() {
    JOptionPane.show...
  }
}

then where you want to schedule your task:

new Timer().schedule(new PopTask(), 1000*60*5);

This kind of timers can also be canceled with cancel() method

Jack
I disagree with the use of a `java.util` timer and timertasks. When dealing with swing apps, it is usually preferable to use the swing Timer.
jjnguy
in this situation there won't be any precious difference between using a `java.util.Timer` or a `javax.swing.Timer` since they will need just a thread each. In anycase it is possible to use the `invokeLater` if you really want to mimic the behaviour. From my personal experience I've used many times `TimerTask` to trigger miscellaneous things with no problems, but it's also a matter of taste :)
Jack
I agree with Jinguy, Swing was designed to have code execute on the EDT. Remeber you are giving advice to a newbie. Yes, most of the time you won't have a problem but why take the chance of having to debug some random problem?
camickr
Yes, that's right. Usually it's better to avoid problems but in this specific case I don't think it will matter so much. The most important thing is to know about EDT and these situations so basically yes, OP should follow your advice :)
Jack
+1  A: 

Read the section from the Swing tutorial on How to Use Timers. When the dialog is displayed you start the Timer. When the dialog is closed you stop the Timer.

A Swing Timer should be used, not a TimerTask so that if the Timer fires the code will be executed on the EDT.

camickr
+1  A: 

Essentially, after the initial option pane is shown, start a Timer. (A javax.swing one)

You will also need a class-level variable indicating if the temp has been entered yet.

JOptionPane.showMessageDialog(...);
tempHasBeenEntered = false;
Timer tim = new Timer(5 * 60 * 1000, new ActionListener() { 
                 public void actionPerformed(ActionEvent e) { 
                     if (!tempHasBeenEntered)
                         JOptionPane.showMessageDialog("Hey, enter the temp!!"); 
                 } 
}
tim.setRepeats(false);
tim.start();

You will need to flip the flag once a user enters a temp into the form.

jjnguy
Thank you for providing the Timer links..It works! I guess for now swing Timer should be a good way to go about things.
Faraz Khan
However, i want it to do it only ONCE...I know I have to use this on the timer setRepeats(false) but where
Faraz Khan
@Faraz, I will update my answer. You will need to save the timer to a variable, yo'll see.
jjnguy
thanks , i did that and works.
Faraz Khan