Erwin Barrato Anog
Erwin Barrato Anog

Reputation: 11

Close dialog box automatically

I have a question about JOptionPane on how to automatically closes dialog box that i can use to alert a user in an event and close automatically after a delay without having to close the dialog?

public class ShutdownComputer
{  
    public static void main(String[]args)  
    {  
        int wholeTimeBeforeShutdown=0;  

        int hour=0;  
        int minute=0;  
        int second=0;  

        try  
        {  
            String a=JOptionPane.showInputDialog(null,"HOUR","HOUR BEFORE SHUTDOWN",JOptionPane.QUESTION_MESSAGE);  
            String b=JOptionPane.showInputDialog(null,"MINUTE","MINUTE BEFORE SHUTDOWN",JOptionPane.QUESTION_MESSAGE);  
            String c=JOptionPane.showInputDialog(null,"SECOND","SECOND BEFORE SHUTDOWN",JOptionPane.QUESTION_MESSAGE);  

            if((a==null)||(a.equals("")))  
            {  
                a="0";  
            }  

            if((b==null)||(b.equals("")))  
            {  
                b="0";  
            }  

            if((c==null)||(c.equals("")))  
            {  
                c="0";  
            }  

            int e=Integer.parseInt(a);  
            int f=Integer.parseInt(b);  
            int g=Integer.parseInt(c);  

            wholeTimeBeforeShutdown=wholeTimeBeforeShutdown+((e*60)*60);  
            wholeTimeBeforeShutdown=wholeTimeBeforeShutdown+(f*60);  
            wholeTimeBeforeShutdown=wholeTimeBeforeShutdown+(g);  

            wholeTimeBeforeShutdown=wholeTimeBeforeShutdown*1000;  


            Thread.sleep(wholeTimeBeforeShutdown);  

            JOptionPane.showMessageDialog(null, "You only have less than 2 minutes left"); 

            Runtime.getRuntime().exec("shutdown -r -f -t 120");  
        }  
        catch(Exception exception)  
        {  
            exception.printStackTrace();  
        }  
    }  
}

Upvotes: 1

Views: 4011

Answers (2)

Holger
Holger

Reputation: 298539

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.concurrent.TimeUnit;

import javax.swing.JDialog;
import javax.swing.JOptionPane;
import javax.swing.Timer;

    final String msg="Shutdown\nWill happen automatically in "; 
    final JOptionPane op=new JOptionPane(null, JOptionPane.INFORMATION_MESSAGE);
    final JDialog d=op.createDialog("Shutdown");
    d.setModal(true);
    // deadline in three hours:
    final long deadline=System.nanoTime()+TimeUnit.HOURS.toNanos(3);
    ActionListener updater=new ActionListener() {
      public void actionPerformed(ActionEvent e)
      {
        long left=deadline-System.nanoTime();
        if(left<=0) {
          d.dispose();
          return;
        }
        String time;
        long t=TimeUnit.NANOSECONDS.toHours(left);
        if(t>0) time=t>1? t+" hours": "one hour";
        else {
          t=TimeUnit.NANOSECONDS.toMinutes(left);
          time=t>1? t+" minutes": "one minute";
        }
        op.setMessage(msg+time);
      }
    };
    updater.actionPerformed(null);//update initial message
    d.pack(); // resize dialog for the updated message
    final Timer t=new Timer(1000, updater);
    t.setInitialDelay(0);
    t.setRepeats(true);
    t.start();
    d.setVisible(true);
    t.stop();
    // do your action

Note that the message made by this code will round down time values, but I think it’s a usable starting point for you.

Upvotes: 1

Pablo
Pablo

Reputation: 11

Probably what you need is to use JOptionPane.setVisible(false). I haven't tried that but the example at the bottom of http://docs.oracle.com/javase/tutorial/uiswing/components/dialog.html can give you a hint.

Upvotes: 0

Related Questions