Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 3 of 3

Thread: Shutdown Timer

  1. #1
    Junior Member
    Join Date
    Jul 2013
    Posts
    18
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Shutdown Timer

    Using the following code how would I start the timer when the button is clicked and how would I set the timer with the input time?

    import java.awt.*;
    import java.awt.event.*;
    import javax.swing.*;
     
    public class JShutdownTimer extends JPanel {
       private static final String INTRO = "intro";
       private static final String USED_BEFORE = "Shutting Down";
       private CardLayout cardLayout = new CardLayout();
       private JLabel countDownLabel = new JLabel("", SwingConstants.CENTER);
       private JLabel lblHours = new JLabel("Hours to shutdown:", SwingConstants.CENTER);
       private JLabel lblMinutes = new JLabel("Minutes to shutdown:", SwingConstants.CENTER);
       private JLabel lblStatus = new JLabel("Status:");
       private JLabel lblWelcome = new JLabel("Welcome", SwingConstants.CENTER);
       private JTextField txtHours = new JTextField();
       private JTextField txtMinutes = new JTextField();
       private JButton btnRun = new JButton("Run");
     
       public JShutdownTimer() {
     
          JPanel introPanel = new JPanel();
          introPanel.setPreferredSize(new Dimension(300, 200));
          introPanel.setLayout(new GridLayout(8, 1));
          lblStatus.setFont(new Font("Dialog", 1, 15));
          introPanel.add(lblWelcome);
     
          // hours label, textbox and validation
          lblHours.setFont(new Font("Dialog", 1, 15));
          introPanel.add(lblHours);
          txtHours.setFont(new Font("Dialog", 1, 15));
          introPanel.add(txtHours);
          txtHours.addKeyListener(new KeyAdapter() {
              public void keyTyped(KeyEvent e) {
                  char c = e.getKeyChar();
                  if (!((c >= '0') && (c <= '9') ||
                          (c == KeyEvent.VK_BACK_SPACE) ||
                          (c == KeyEvent.VK_DELETE))) {
                      getToolkit().beep();
                      e.consume();
                  }
              }
          });
     
          // minutes label, textbos and validation
          lblMinutes.setFont(new Font("Dialog", 1, 15));
          introPanel.add(lblMinutes);
          txtMinutes.setFont(new Font("Dialog", 1, 15));
          introPanel.add(txtMinutes);
          txtMinutes.addKeyListener(new KeyAdapter() {
              public void keyTyped(KeyEvent e) {
                  char c = e.getKeyChar();
                  if (!((c >= '0') && (c <= '9') ||
                          (c == KeyEvent.VK_BACK_SPACE) ||
                          (c == KeyEvent.VK_DELETE))) {
                      getToolkit().beep();
                      e.consume();
                  }
              }
          });
     
          // button
          btnRun.setFont(new Font("Dialog", 1, 15));
          introPanel.add(btnRun);
          RunClass runclass = new RunClass();
          btnRun.addActionListener(runclass);
     
          JPanel introSouthPanel = new JPanel();
          lblStatus.setFont(new Font("Dialog", 1, 15));
          introSouthPanel.add(lblStatus);
          countDownLabel.setFont(new Font("Dialog", 1, 15));
          introSouthPanel.add(countDownLabel);
          introPanel.add(introSouthPanel, BorderLayout.SOUTH);
     
          JPanel usedBeforePanel = new JPanel(new BorderLayout());
          usedBeforePanel.setBackground(Color.pink);
          usedBeforePanel.add(new JLabel("Used Before", SwingConstants.CENTER));
     
          setLayout(cardLayout);
          add(introPanel, INTRO);
          add(usedBeforePanel, USED_BEFORE);
     
          new HurdlerTimer(this).start();
       }
     
       private class RunClass implements ActionListener {
           @Override
           public void actionPerformed(ActionEvent e) {
               if (e.getSource() == btnRun) {
                   int intHours = Integer.valueOf(txtHours.getText()) * 60;
                   int intMinutes = Integer.valueOf(txtMinutes.getText());
                   int intTotalMinutes = intHours + intMinutes;
                   Shutdown.shutdown(intTotalMinutes);
               }
           }
       }
     
       private static void createAndShowUI() {
          JFrame frame = new JFrame("JShutdownTimer");
          frame.getContentPane().add(new JShutdownTimer());
          frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
          frame.pack();
          frame.setLocationRelativeTo(null);
          frame.setVisible(true);
       }
     
       public static void main(String[] args) {
          java.awt.EventQueue.invokeLater(new Runnable() {
             public void run() {
                createAndShowUI();
             }
          });
       }
     
       public void setCountDownLabelText(String text) {
          countDownLabel.setText(text);
       }
     
       public void showNextPanel() {
          cardLayout.next(this);
       }
    }
    class HurdlerTimer {
       private static final int TIMER_PERIOD = 1000;
       protected static final int MAX_COUNT = 10;
       private JShutdownTimer welcome; // holds a reference to the Welcome class
       private int count;
     
       public HurdlerTimer(JShutdownTimer welcome) {
          this.welcome = welcome; // initializes the reference to the Welcome class.
          String text = "(" + (MAX_COUNT - count) + ") seconds left";
          welcome.setCountDownLabelText(text);
       }
     
       public void start() {
          new Timer(TIMER_PERIOD, new ActionListener() {
             @Override
             public void actionPerformed(ActionEvent e) {
                if (count < MAX_COUNT) {
                   count++;
                   String text = "(" + (MAX_COUNT - count) + ") seconds left";
                   welcome.setCountDownLabelText(text); // uses the reference to Welcome
                } else {
                   ((Timer) e.getSource()).stop();
                   welcome.showNextPanel();
                }
             }
          }).start();
       }
     
    }


  2. #2
    Member
    Join Date
    Jul 2013
    Posts
    219
    Thanks
    0
    Thanked 18 Times in 17 Posts

    Default Re: Shutdown Timer

    Hello.
    Timer object expects time in milli seconds.
    You should remove the statement new HurdlerTimer(this).start() from where you kept it originally and write it before Shutdown.shutdown() in the RunClass' actionPerformed().
    The statement should be new HurdlerTimer(JShutdownTimer.this).start(intTotalMi nutes*60*1000).
    Change the start() of HurdlerTimer class to start(int timer).
    Then the next statement will be new Timer(timer,....).

    Let me know if it will work.

    Thanks,
    Syed.

  3. #3
    Junior Member
    Join Date
    Jul 2013
    Posts
    18
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Shutdown Timer

    Thanks but the code's going all screwy, looks like I'll just have to leave the timer out.

Similar Threads

  1. [SOLVED] Create shutdown or Sleep using java
    By SCC.renz in forum What's Wrong With My Code?
    Replies: 1
    Last Post: March 27th, 2013, 10:51 AM
  2. Remote shutdown help needed!
    By dmarsh12 in forum What's Wrong With My Code?
    Replies: 1
    Last Post: November 26th, 2012, 07:31 PM
  3. ShutDown from JButton
    By mija in forum What's Wrong With My Code?
    Replies: 7
    Last Post: July 7th, 2012, 01:08 PM
  4. Shutdown Remote Computer using java....
    By pupivama in forum Member Introductions
    Replies: 1
    Last Post: May 8th, 2012, 07:44 AM
  5. remote system shutdown
    By prince joseph in forum What's Wrong With My Code?
    Replies: 0
    Last Post: March 28th, 2010, 02:35 AM

Tags for this Thread