1

I'm trying to make a java program, with GUI, that will let the user input the time he wants to wait until the machine will shutdown, restart or sleep. I managed to make the GUI and implemented the commands to shutdown, restart or sleep, but i don't know how to make the timer.

enter image description here

Here is what i did so far :

import java.io.IOException;
import java.util.logging.Level;
import java.util.logging.Logger;


public class ContactEditorUI extends javax.swing.JFrame {

    public ContactEditorUI() {
        initComponents();
    }

    @SuppressWarnings("unchecked")

    private void initComponents() {

        buttonGroup2 = new javax.swing.ButtonGroup();
        jInternalFrame1 = new javax.swing.JInternalFrame();
        jRadioButton1 = new javax.swing.JRadioButton();
        jRadioButton2 = new javax.swing.JRadioButton();
        jRadioButton3 = new javax.swing.JRadioButton();
        jLabel1 = new javax.swing.JLabel();
        jButton1 = new javax.swing.JButton();
        jTextField1 = new javax.swing.JTextField();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

        jInternalFrame1.setVisible(true);

        jRadioButton1.setText("Shut down");
        jRadioButton1.addContainerListener(new java.awt.event.ContainerAdapter() {
            public void componentAdded(java.awt.event.ContainerEvent evt) {
                jRadioButton1ComponentAdded(evt);
            }
        });
        jRadioButton1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jRadioButton1ActionPerformed(evt);
            }
        });

        jRadioButton2.setText("Restart");
        jRadioButton2.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jRadioButton2ActionPerformed(evt);
            }
        });

        jRadioButton3.setText("Sleep");
        jRadioButton3.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jRadioButton3ActionPerformed(evt);
            }
        });

        jLabel1.setText("Please insert the time :");

        jButton1.setText("OK");
        jButton1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton1ActionPerformed(evt);
            }
        });

        jTextField1.setFont(new java.awt.Font("Tahoma", 0, 14)); // NOI18N
        jTextField1.setHorizontalAlignment(javax.swing.JTextField.CENTER);
        jTextField1.setText("00:00:00");

        javax.swing.GroupLayout jInternalFrame1Layout = new javax.swing.GroupLayout(jInternalFrame1.getContentPane());
        jInternalFrame1.getContentPane().setLayout(jInternalFrame1Layout);
        jInternalFrame1Layout.setHorizontalGroup(
            jInternalFrame1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jInternalFrame1Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jInternalFrame1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jLabel1)
                    .addGroup(jInternalFrame1Layout.createSequentialGroup()
                        .addComponent(jTextField1, javax.swing.GroupLayout.PREFERRED_SIZE, 87, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(50, 50, 50)
                        .addComponent(jButton1))
                    .addComponent(jRadioButton1)
                    .addComponent(jRadioButton3)
                    .addComponent(jRadioButton2))
                .addContainerGap(140, Short.MAX_VALUE))
        );
        jInternalFrame1Layout.setVerticalGroup(
            jInternalFrame1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jInternalFrame1Layout.createSequentialGroup()
                .addComponent(jLabel1)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(jInternalFrame1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                    .addComponent(jButton1)
                    .addGroup(jInternalFrame1Layout.createSequentialGroup()
                        .addComponent(jTextField1)
                        .addGap(2, 2, 2)))
                .addComponent(jRadioButton1)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(jRadioButton2)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(jRadioButton3)
                .addContainerGap(32, Short.MAX_VALUE))
        );

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jInternalFrame1)
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jInternalFrame1)
        );

        pack();
    }                      

    private void jRadioButton1ActionPerformed(java.awt.event.ActionEvent evt) {                                              
       buttonGroup.add(jRadioButton1);
         Runtime runtime = Runtime.getRuntime();
            try {
                //wait();
                Process proc = runtime.exec("shutdown -s");
            } catch (IOException ex) {
                Logger.getLogger(ContactEditorUI.class.getName()).log(Level.SEVERE, null, ex);
            }
    }                                             

    private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {                                         

    }                                        

    private void jRadioButton2ActionPerformed(java.awt.event.ActionEvent evt) {                                              

        buttonGroup.add(jRadioButton2);
        Runtime runtime = Runtime.getRuntime();
            try {
                //wait();
                Process proc = runtime.exec("shutdown -r");
            } catch (IOException ex) {
                Logger.getLogger(ContactEditorUI.class.getName()).log(Level.SEVERE, null, ex);
            }
    }                                             

    private void jRadioButton3ActionPerformed(java.awt.event.ActionEvent evt) {                                              

        buttonGroup.add(jRadioButton3);
        Runtime runtime = Runtime.getRuntime();
            try {
                //wait();
                Process proc = runtime.exec("shutdown -h");
            } catch (IOException ex) {
                Logger.getLogger(ContactEditorUI.class.getName()).log(Level.SEVERE, null, ex);
            }
    }                                             

    private void jRadioButton1ComponentAdded(java.awt.event.ContainerEvent evt) {                                             

    }                                            


    public static void main(String args[]) {

        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                new ContactEditorUI().setVisible(true);
            }
        });
    }


    private javax.swing.ButtonGroup buttonGroup;
    private javax.swing.JButton jButton1;
    private javax.swing.JInternalFrame jInternalFrame1;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JRadioButton jRadioButton1;
    private javax.swing.JRadioButton jRadioButton2;
    private javax.swing.JRadioButton jRadioButton3;
    private javax.swing.JTextField jTextField1;

}

All the help will be very appreciated.

DanielBarbarian
  • 5,093
  • 12
  • 35
  • 44
Steve
  • 13
  • 2
  • Isn't the Timer the whole point of your program? And have you researched Timer() class in Java? – Kleo G May 03 '18 at 10:53
  • yeah, the Timer is the whole point, and I have researched Timer() class. But nothing that i wrote seemed to work.. – Steve May 03 '18 at 10:55
  • What have you written in regards to the Timer aspect so far? – Kleo G May 03 '18 at 10:56
  • int count = (int)(Double.parseDouble(jTextField1.getText())); TimeClass tc = new TimeClass(count); timer = new Timer(1000, tc) timer.start(); – Steve May 03 '18 at 11:00
  • problem is that i want it so that the user can input hh:mm:ss and i dont seem to find out how – Steve May 03 '18 at 11:03
  • As a side note, you could just case the jTextField1 straight to an Int instead of a Double (with the necessary exception handling and idiot proof checks that is), and you would need to mention that in your question, I will leave an answer now – Kleo G May 03 '18 at 11:08

2 Answers2

0

I think these two questions will help you or at least give you a clue:

JDialog timeout when idle
Timer on a java swing jdialog

Vasil
  • 60
  • 1
  • 11
  • 1
    Please to not post link-only answers. – Syn May 03 '18 at 11:14
  • Why should I copy/paste already given answers in other stackoverflow questions? Looks like bad googling experience to find already provided solution, just should be adopted. I'll keep in mind you note about the link-only answers in future. – Vasil May 03 '18 at 11:23
  • You should not copy/paste the answers, but you could attempt to provide a brief summary of the questions and the answers you linked. This will also help users in the future more easily identify if your answer also applies to their problem. – Syn May 03 '18 at 11:25
0

You would need to use Java's Timer class. Within this there's a method called schedule() with different signatures allowing for a variety of uses.

     Timer timer = new Timer();
     int start=10000; //millisecond 10 seconds=10000
     int delay=1000; // millisecond 1 second
     timer.schedule(new TimerTask()
       {
        public void run() {
            // what happens when the timer repeats
        }
      },start,delay);

This piece of code was found originally here. This is a new answer as to relate to a bit of context to your question.

This will start a new Timer when it is called. To be able to let the user set the specified time, the schedule method takes that in as a parameter (the date that is input would need to be converted into the start variable).

Use the Java documentation found here to be able to modify this answer to suit you, as schedule can be used in a few different ways.

Kleo G
  • 247
  • 1
  • 4
  • 20
  • Thank you for the help you provided, it helped. But i am a newcomer to java, since in our school we didn't do much. I wanted to ask, if i should implement the time.schedule() for all the radio buttons that i added? or should it be implemented in another form? – Steve May 03 '18 at 11:46
  • Thank you, if it helps would it be okay to accept the answer and upvote. It may also help any other person looking for the same thing. That's okay, we are all here to learn. So, if what you are doing in the time scheduler will do the same thing for each radio button, make a method and call that method from wherever you need it. – Kleo G May 03 '18 at 14:51
  • Thanks so much! I think i figured it out. Now it’s just time to test what i have in mind. Also, i will follow up once it’s up and running and will complete the code, so that it may help if needed. – Steve May 04 '18 at 11:02
  • Nice! I would like to see how you progress, you can add the update to this answer and I'll accept the edit, save from adding it to the question. – Kleo G May 04 '18 at 11:12