Search code examples
javaswingtimejoptionpane

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.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)?


Solution

  • 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.