Wednesday, June 11, 2008

MEMBUAT SHUTDOWN TIMER DENGAN MEMANFAATKAN PROGRAM SHUTDOWN.EXE WINDOWS

,
Pada Windows XP, terdapat aplikasi bawaan untuk shutdown Windows itu sendiri yaitu shutdown.exe yang terletak di C:\WINDOWS\system32. Sebetulnya aplikasi shutdown bawaan Windows ini dapat dijalankan dengan menggunakan command prompt. Tetapi biasanya orang menjalankan dengan menjalankan menu start --> Turn Off Computer... --> shutdown. Dan hasilnya, komputer pasti langsung mati. Kita tidak dapat mengeset parameter yang ada pada shutdown.exe. Nah, bagaimana caranya jika mau mematikan komputer 1 jam lagi misalnya? Kita dapat menjalankan command prompt lalu mengetikkan shutdown -s -t 3600. -s artinya shutdown sedangkan -t 3600 berarti mengeset waktu 1 jam sebelum komputer mati. Cara ini dapat dilakukan tetapi mungkin tidak nyaman bagi sebagian orang. Untuk itu saya mencoba membuat aplikasi untuk menjalankan shutdown.exe milik Windows XP dengan Java. Berikut ini sorce codenya :


[sourcecode language='java']
/*
* Main.java
*
* Created on June 4, 2008, 6:47 AM
*/

package shutdowntimer;

import java.io.IOException;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;

/**
*
* @author FX. Budi Ariyanto
*/

public class Main extends javax.swing.JFrame {

/** Creates new form Main */

public Main() throws ClassNotFoundException, InstantiationException, IllegalAccessException, UnsupportedLookAndFeelException
{
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
initComponents();
}

private ProcessBuilder pb=null;
private Integer hour=1,minute=0,second=0,timer=3600;

/** This method is called from within the constructor to

* initialize the form.

* WARNING: Do NOT modify this code. The content of this method is

* always regenerated by the Form Editor.

*/

@SuppressWarnings("unchecked")

//

private void initComponents() {

jPanel1 = new javax.swing.JPanel();
jLabel1 = new javax.swing.JLabel();
jLabel2 = new javax.swing.JLabel();
btnGo = new javax.swing.JButton();
btnCancel = new javax.swing.JButton();
spnHour = new javax.swing.JSpinner();
spnMinute = new javax.swing.JSpinner();
spnSeconds = new javax.swing.JSpinner();
cmbAction = new javax.swing.JComboBox();
jLabel3 = new javax.swing.JLabel();
jLabel4 = new javax.swing.JLabel();
jLabel5 = new javax.swing.JLabel();
jMenuBar1 = new javax.swing.JMenuBar();
menuFile = new javax.swing.JMenu();
itemExit = new javax.swing.JMenuItem();
menuAbout = new javax.swing.JMenu();
setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
setTitle("ShutdownTimer 1.0");
jLabel1.setText("Set Timer :");
jLabel2.setText("Action :");
btnGo.setText("Go!");
btnGo.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
btnGoActionPerformed(evt);

}

});

btnCancel.setText("Cancel Action");
btnCancel.setEnabled(false);
btnCancel.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {

btnCancelActionPerformed(evt);

}

});

spnHour.setValue(1);
cmbAction.setMaximumRowCount(4);
cmbAction.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Shutdown", "Restart", "Log Off" }));
jLabel3.setText("hour(s)");
jLabel4.setText("minute(s)");
jLabel5.setText("second(s)");
javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
jPanel1.setLayout(jPanel1Layout);
jPanel1Layout.setHorizontalGroup(
jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel1Layout.createSequentialGroup()
.addGap(21, 21, 21)
.addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel1Layout.createSequentialGroup()
.addComponent(jLabel1)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(spnHour, javax.swing.GroupLayout.DEFAULT_SIZE, 36, Short.MAX_VALUE)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(jLabel3)
.addGap(16, 16, 16)
.addComponent(spnMinute, javax.swing.GroupLayout.PREFERRED_SIZE, 37, javax.swing.GroupLayout.PREFERRED_SIZE)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(jLabel4)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
.addComponent(spnSeconds, javax.swing.GroupLayout.PREFERRED_SIZE, 35, javax.swing.GroupLayout.PREFERRED_SIZE)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(jLabel5))
.addGroup(jPanel1Layout.createSequentialGroup()
.addComponent(jLabel2)
.addGap(18, 18, 18)
.addComponent(cmbAction, javax.swing.GroupLayout.PREFERRED_SIZE, 101, javax.swing.GroupLayout.PREFERRED_SIZE)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(btnGo)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(btnCancel)))
.addContainerGap())

);

jPanel1Layout.setVerticalGroup(
jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(jPanel1Layout.createSequentialGroup()
.addContainerGap()
.addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(jLabel1)
.addComponent(jLabel5)
.addComponent(spnHour, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(jLabel4)
.addComponent(spnSeconds, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(spnMinute, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(jLabel3))
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(jLabel2)
.addComponent(cmbAction, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(btnGo)
.addComponent(btnCancel))
.addGap(119, 119, 119))

);

menuFile.setText("File");
itemExit.setAccelerator(javax.swing.KeyStroke.getKeyStroke(java.awt.event.KeyEvent.VK_F4, java.awt.event.InputEvent.ALT_MASK));
itemExit.setText("Exit");
itemExit.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {

itemExitActionPerformed(evt);

}
});

menuFile.add(itemExit);
jMenuBar1.add(menuFile);
menuAbout.setText("About");
menuAbout.addMouseListener(new java.awt.event.MouseAdapter() {
public void mouseClicked(java.awt.event.MouseEvent evt) {
menuAboutMouseClicked(evt);
}

});

jMenuBar1.add(menuAbout);
setJMenuBar(jMenuBar1);
javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
getContentPane().setLayout(layout);
layout.setHorizontalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
);
layout.setVerticalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, 75, Short.MAX_VALUE)
);

java.awt.Dimension screenSize = java.awt.Toolkit.getDefaultToolkit().getScreenSize();

setBounds((screenSize.width-368)/2, (screenSize.height-130)/2, 368, 130);

}//


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

hour = (Integer) spnHour.getValue()*3600;
minute = (Integer) spnMinute.getValue()*60;
second = (Integer) spnSeconds.getValue();
timer = hour + minute + second;

String comment = "Komputer ini akan "+cmbAction.getSelectedItem()+" dalam waktu "+spnHour.getValue()+" jam "+spnMinute.getValue()+" menit "+spnSeconds.getValue()+" detik.";

if(cmbAction.getSelectedItem().equals("Shutdown"))
{
pb = new ProcessBuilder("C:/WINDOWS/system32/shutdown.exe","-s","-t",timer.toString(),"-c",comment);

try
{
pb.start();

}
catch (IOException ex)
{
Logger.getLogger(Main.class.getName()).log(Level.SEVERE, null, ex);
}

}
else if(cmbAction.getSelectedItem().equals("Restart"))
{
pb = new ProcessBuilder("C:/WINDOWS/system32/shutdown.exe","-r","-t",timer.toString(),"-c",comment);

try
{
pb.start();
}
catch (IOException ex)
{
Logger.getLogger(Main.class.getName()).log(Level.SEVERE, null, ex);
}

}
else
{
pb = new ProcessBuilder("C:/WINDOWS/system32/shutdown.exe","-l","-t",timer.toString(),"-c",comment);

try
{
pb.start();
}
catch (IOException ex)
{
Logger.getLogger(Main.class.getName()).log(Level.SEVERE, null, ex);
}
}

btnCancel.setEnabled(true);
btnGo.setEnabled(false);
spnHour.setEnabled(false);
spnMinute.setEnabled(false);
spnSeconds.setEnabled(false);
cmbAction.setEnabled(false);

}

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

pb = new ProcessBuilder("C:/WINDOWS/system32/shutdown.exe","-a");

try {

pb.start();

} catch (IOException ex) {

Logger.getLogger(Main.class.getName()).log(Level.SEVERE, null, ex);

}

btnCancel.setEnabled(false);
btnGo.setEnabled(true);
spnHour.setEnabled(true);
spnMinute.setEnabled(true);
spnSeconds.setEnabled(true);
cmbAction.setEnabled(true);

}

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

System.exit(0);

}

private void menuAboutMouseClicked(java.awt.event.MouseEvent evt) {

new About().setVisible(true);

}

/**

* @param args the command line arguments

*/

public static void main(String args[]) {
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
try {

new Main().setVisible(true);

} catch (ClassNotFoundException ex) {

Logger.getLogger(Main.class.getName()).log(Level.SEVERE, null, ex);

} catch (InstantiationException ex) {

Logger.getLogger(Main.class.getName()).log(Level.SEVERE, null, ex);

} catch (IllegalAccessException ex) {

Logger.getLogger(Main.class.getName()).log(Level.SEVERE, null, ex);

} catch (UnsupportedLookAndFeelException ex) {

Logger.getLogger(Main.class.getName()).log(Level.SEVERE, null, ex);

}

}

});

}

// Variables declaration - do not modify

private javax.swing.JButton btnCancel;
private javax.swing.JButton btnGo;
private javax.swing.JComboBox cmbAction;
private javax.swing.JMenuItem itemExit;
private javax.swing.JLabel jLabel1;
private javax.swing.JLabel jLabel2;
private javax.swing.JLabel jLabel3;
private javax.swing.JLabel jLabel4;
private javax.swing.JLabel jLabel5;
private javax.swing.JMenuBar jMenuBar1;
private javax.swing.JPanel jPanel1;
private javax.swing.JMenu menuAbout;
private javax.swing.JMenu menuFile;
private javax.swing.JSpinner spnHour;
private javax.swing.JSpinner spnMinute;
private javax.swing.JSpinner spnSeconds;

// End of variables declaration

}
[/sourcecode]

0 comments to “MEMBUAT SHUTDOWN TIMER DENGAN MEMANFAATKAN PROGRAM SHUTDOWN.EXE WINDOWS”

Post a Comment