开发者

How to create a dialogbox that will pop up after 5 minutes of idleness by the user? (java)

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.showM开发者_JAVA技巧essageDialog(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)?


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


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.


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜