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