a project run@timer, j2se,

66
Saurabh Jain Assistant. Professor,CSE OCT BHOPAL ORIENTAL COLLEGE OF TECHNOLOGY, BHOPAL (Formerly known as Thakral College of Technology, Bhopal) Approved by AICTE New Delhi & Govt. of M.P. and Affiliated to Rajiv Gandhi Proudyogiki Vishwavidhyalaya Bhopal (M.P.) DEPARTMENT OF COMPUTER SCIENCE & ENGINEERING CERTIFICATE This is to Certify that the Minor Project Entitled Run@Timer” Being Submitted By Swapnil Dubey and Radhika Shrivastava in Partial fulfillment of the Requirement for the Award Of B.E Degree In Computer Science & Engineering To Oriental College Of Technology, Bhopal(M.P) is a Record Of Bonafide work done by her under my Guidance. Prof. Rachana Mishra Head of Department, CSE OCT BHOPAL

Upload: swapnil-dubey

Post on 28-Jan-2015

111 views

Category:

Engineering


2 download

DESCRIPTION

RUN@TIMER is an application created by me on pure innovation concept. And I am glad to announce that I was asked a number of questions on it, but I passed it all and hoping for good marks now!

TRANSCRIPT

Page 1: A Project Run@Timer, J2SE,

Saurabh JainAssistant. Professor,CSE

OCT BHOPAL

ORIENTAL COLLEGE OF TECHNOLOGY, BHOPAL(Formerly known as Thakral College of Technology, Bhopal)

Approved by AICTE New Delhi & Govt. of M.P. and Affiliated to Rajiv Gandhi Proudyogiki Vishwavidhyalaya Bhopal (M.P.)

DEPARTMENT OF COMPUTER SCIENCE & ENGINEERING

CERTIFICATE

This is to Certify that the Minor Project Entitled “Run@Timer” Being

Submitted By Swapnil Dubey and Radhika Shrivastava in Partial fulfillment

of the Requirement for the Award Of B.E Degree In Computer Science &

Engineering To Oriental College Of Technology, Bhopal(M.P) is a Record Of

Bonafide work done by her under my Guidance.

Prof. Rachana MishraHead of Department, CSE

OCT BHOPAL

Page 2: A Project Run@Timer, J2SE,

ACKNOWLEDGEMENT

I am heartily thankful to the Management of Oriental College of Technology for providing

me all the facilities and infrastructure to take my work to the final stage.

I would like to express my deep sense of respect and gratitude towards my advisor and guide

Saurabh Jain, A.P,CSE Deptt. Oriental College of Technology, Bhopal who has given me

an opportunity to work under him. He has been a constant source of inspiration throughout

my work. His invaluable knowledge and innovative ideas helped me to take the work to the

final stage.

I express my respect to Dr. Kavita Burse, Director, Oriental College of Technology for her

constant encouragement and invaluable advice in every aspect of my academic life.

I would like to express my sincere thanks to Prof. Rachana Mishra, Head, Computer

Science Engineering Department, Oriental College of Technology and all faculty members of

Computer Science and Engineering Department for their support and guidance.

I am especially thankful to our Parents and our siblings for their love, sacrifice and support

on every path of my life..

Last but not the least I am extremely thankful to all who have directly or indirectly helped me

for the completion of my work.

Page | 2

Page 3: A Project Run@Timer, J2SE,

INDEX

1. Abstract……………………………………..2. Introduction…………………………………3. Brief explanation………………………………..4. Software requirement specification…………..

i) Requirementii) Hardwareiii) Scopeiv) Tools used

5. Software engineering methods used……..6. Related and proposed scenario…………….7. Limitations and further scope……………..8. Source codes snapshots……………………… 9. Conclusions………………………………….10. References……………………………………

Page | 3

Page 4: A Project Run@Timer, J2SE,

ABSTRACT

We now a days are so busy in handling things on our computer systems that we often miss a task during our work, and then realize after sometime that if we’d have done this task results would have been different. That’s where the application developed by me and my team comes into existence. Run@timer is a timer application developed in J2SE platform and takes time and task to be done as input and then at given time performs that task. This project report intends to give an overview of Run@Timer, its uses and techniques. It also attempts to identify the requirements of a good algorithm to be followed and briefly reflects on which methods to be used to make a good project on this topic and which techniques are more suitable for which application.

Page | 4

Page 5: A Project Run@Timer, J2SE,

INTRODUCTION

Timer in Java is a utility class which is used to schedule tasks for both one time and repeated execution. Timer is similar to alarm facility many people use in mobile phone.

Just like you can have one time alarm or repeated alarm, you can use java.util.Timer to schedule one time task or repeated task.

In fact we can implement a Reminder utility using Timer in Java and that's what we are going to see in this example of Timer in Java. Two classes java.util.Timer and java.util.TimerTask is used to schedule jobs in Java and forms Timer API.

TimerTask is actual task which is executed by Timer. Similar to Thread in Java, TimerTask also implements Runnable interface and overrides run method to specify task details.

Timer and TimerTask example in Java Timer in Java is a utility class form java.util package which provides facility to schedule task at any time in future.

Page | 5

Page 6: A Project Run@Timer, J2SE,

As I said earlier, Timer is analogues to alarm clock you setup in your smartphone. Just like alarm can be either one time or recurring, You can also schedule task for one time and recurring time interval using Timer API.

Timer provides method to schedule Task where task is instance of TimerTask class, which implements Runnable interface and overrides run() method to define task which is called on scheduled time.

Page | 6

Page 7: A Project Run@Timer, J2SE,

SYNOPSIS

Project Name: RUN@TIMER

Project Members:

This Project is done in a group of two people. Project members are:

1. Swapnil Dubey

2. Radhika Shrivastava

Page | 7

Page 8: A Project Run@Timer, J2SE,

BRIEF EXPLANATION

What is Timer and Timertask() in Java?

Timer class in Java maintains a background Thread (this could be either daemon thread or user thread, based on how you created your Timer object), also called as timer's task execution thread.

For each Timer there would be corresponding task processing Thread which run scheduled task at specified time.

If your Timer thread is not daemon then it will stop your application from exits until it completes all schedule task. Its recommended that TimerTask should not be very long otherwise it can keep this thread busy and not allow other scheduled task to get completed. This can delay execution of other scheduled task, which may queue up and execute in quick succession once offending task completed.

Page | 8

Page 9: A Project Run@Timer, J2SE,

Difference between Timer and TimerTask in Java:

As seen , I have seen programmers getting confused between Timer and TimerTask, which is quite unnecessary because these two are altogether different. You just need to remember:

1) Timer in Java schedules and execute TimerTask which is an implementation of Runnable interface and overrides run method to defined actual task performed by that TimerTask.

2) Both Timer and TimerTask provides cancel() method. Timer's cancel() method cancels whole timer while TimerTask's one cancels only a particular task. I think this is the wroth noting difference between Timer and TimerTask in Java.

Page | 9

Page 10: A Project Run@Timer, J2SE,

Canceling Timer in Java

Now talking about the cancelation of Timer task in java, You can cancel Java Timer by calling cancel() method of java.util.Timer class, this would result in following:

1) Timer will not cancel any currently executing task.

2) Timer will discard other scheduled task and will not execute them.

3) Once currently executing task will be finished, timer thread will terminate gracefully.

4) Calling Timer.cancel() more than one time will not affect. second call will be ignored.

In addition to cancelling Timer, You can also cancel individual TimerTask by using cancel() method of TimerTask itself.

Page | 10

Page 11: A Project Run@Timer, J2SE,

Important points on Timer and TimerTask in Java

Now as we have got a knowledge about Timer and TimerTask in Java, we need to remember the following points:

1. One Thread will be created corresponding ot each Timer in Java, which could be either daemon or user thread.

2. You can schedule multiple TimerTask with one Timer.

3. You can schedule task for either one time execution or recurring execution.

4. TimerTask.cancel() cancels only that particular task, while Timer.cancel() cancel all task scheduled in Timer.

5. Timer in Java will throw IllegalStateException if you try to schedule task on a Timer which has been cancelled or whose Task execution Thread has been terminated.

Page | 11

Page 12: A Project Run@Timer, J2SE,

When there is a need to trigger a task automatically based on time we should schedule it using a timer api. The requirement can vary from a single execution on a fixed time to high complex recurring event. For example take our regular alarm clock where we fix a time and it beeps coming day on the fixed time. Similarly a high complex example would be the MS outlook where we can schedule events with a different combination.

Following are the things in consideration

When does the event starts (first time) execution – It may be a day / date / time in future.

Is it a one time event or a recurring event?

If recurring, what is the periodicity? –

In recurrence there are two types, like all the events will be on a fixed time which is planned at the time of scheduling. It may be daily / weekly / monthly / yearly / a day of a week / alternate days / only working days.

Second type is, the recurring event can start after the completion of previous occurrence. There can be a delay between events.

Page | 12

Page 13: A Project Run@Timer, J2SE,

When does it end, after a constant number of executions or after a time is reached like till Friday only.

java.util.Timer and TimerTask :

It gives facility to schedule a task for future execution in a background thread.

Task can be scheduled for single or recurring execution.In recurrence, consecutive tasks will have regular intervals.

Timer has got a cancel method using which all the scheduled tasks can be cancelled.

Timer can be chosen to run as a daemon thread.

It can be scheduled to start with respect to current time using fixed-delay.

TimerTask is a thread handle using which we register tasks with the timer.

Page | 13

Page 14: A Project Run@Timer, J2SE,

Important Variations:

This api provides two important variations that needs to be noted.

Fixed-delay

Each task execution is scheduled relative to the actual execution time of the previous execution. So what is actual execution?

This says that, there is no guarantee that the task will be executed at the pre-planned time and it may be delayed due to garbage collection or some background activity.

We should also not confuse with triggering of task and completion of task.

Triggering is the moment at which the Timer fires start a task. If a thread is asked to send email, it may wait for access to the resource like mail server.

Only after the email is sent this task is considered to be completed. We need to have this duration in mind when we schedule recurrence tasks.

If the recurrence time is shorter and already the previous task is not completed, the subsequent tasks will join the queue.

Page | 14

Page 15: A Project Run@Timer, J2SE,

Fixed-rate

Here each recurring tasks are scheduled relative to the start time of the timer.

If there is any delay in execution subsequent executions will occur in quick succession to catch up with the original scheduled time.

In previous case, subsequent execution will depend on just previous execution time.

Page | 15

Page 16: A Project Run@Timer, J2SE,

Project scope:

This project is developed for anybody who is busy doing multiple tasks on his system and wants an application to run at a specific time but has a chance that he might not remember to start it. Hence by using it he can save his time, remain stress free, do his work and leave rest on our developed application.

Methodology:

User needs to run the application. The user has a option of “select file/Program” he wants to run at the specific time. If the user successfully selects it, the application asks him the time at which he want to run that file/program. And after desired value when he clicks OK then the timer starts and then after desired time given by user the selected file/program execution takes place .

Page | 16

Page 17: A Project Run@Timer, J2SE,

Software Requirements:

Netbeans

Hardware Requirements:

Processor: Preferably 1.0 GHz or greater.

RAM: 512 MB or Greater

Future Enhancements:

To make it available at Windows Market so that the common people can also use it and take maximum benefit of this application.

Page | 17

Page 18: A Project Run@Timer, J2SE,

SCREENSHOTS OF WORKING APPLICATION(USER MANUAL) :

1.When the application starts it looks like this. User sees a window in which the name of application Run@timer is displayed and also option to Select file /Program is there. The look and feel of the application is made very easy to use and user friendly.

Page | 18

Page 19: A Project Run@Timer, J2SE,

2.Then to start the application, user has to click the Select File/Program button.

Page | 19

Page 20: A Project Run@Timer, J2SE,

3. As soon as the user clicks the button, a File chooser window opens and we select file.

Page | 20

Page 21: A Project Run@Timer, J2SE,

4. When we open it we can browse anywhere for file in the computer. As in this example user is selecting music folder and then a song from that folder.

Page | 21

Page 22: A Project Run@Timer, J2SE,

Page | 22

Page 23: A Project Run@Timer, J2SE,

5. As soon as he clicks the open button, he returns to the main window and then he is asked to put the timer.

Page | 23

Page 24: A Project Run@Timer, J2SE,

6. The user selects the time, as in this example , he selected 1 minute:

Page | 24

Page 25: A Project Run@Timer, J2SE,

7. Then he clicks Strt which is the button ehich starts the timer of the application:

Page | 25

Page 26: A Project Run@Timer, J2SE,

8. The timer starts and the second window opens where he can see the time getting reduced second by second:

Page | 26

Page 27: A Project Run@Timer, J2SE,

Page | 27

Page 28: A Project Run@Timer, J2SE,

Page | 28

Page 29: A Project Run@Timer, J2SE,

9. When the timer time which was given by user completes, a message is displayed which says successfully executed and then, we get this screen.

Page | 29

Page 30: A Project Run@Timer, J2SE,

10.As the user selected the song , we can see that the song is played as soon as the time completes.

Page | 30

Page 31: A Project Run@Timer, J2SE,

Coding Screenshots to prove the project is a pure innovational concept and is not copied from anywhere:

For the first frame:

1.

Page | 31

Page 32: A Project Run@Timer, J2SE,

Page | 32

Page 33: A Project Run@Timer, J2SE,

Page | 33

Page 34: A Project Run@Timer, J2SE,

For the second frame:

Page | 34

Page 35: A Project Run@Timer, J2SE,

Page | 35

Page 36: A Project Run@Timer, J2SE,

Page | 36

Page 37: A Project Run@Timer, J2SE,

Page | 37

Page 38: A Project Run@Timer, J2SE,

Page | 38

Page 39: A Project Run@Timer, J2SE,

Detailed and clear coding for the first Frame of application:

/* * To change this license header, choose License Headers in Project Properties. * To change this template file, choose Tools | Templates * and open the template in the editor. */

/** * * @author swapnil dubey */import java.awt.Desktop;import java.awt.Toolkit;import java.awt.event.WindowEvent;import java.io.File;import java.net.URL;import javax.swing.JFileChooser;import javax.swing.JOptionPane;public class FirstFrame extends javax.swing.JFrame {

/** * Creates new form FirstFrame */ public FirstFrame() { initComponents(); }

/** * 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") // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents private void initComponents() {

jLabel1 = new javax.swing.JLabel(); jTextField1 = new javax.swing.JTextField(); jButton1 = new javax.swing.JButton(); jSpinner1 = new javax.swing.JSpinner(); jSpinner2 = new javax.swing.JSpinner();

Page | 39

Page 40: A Project Run@Timer, J2SE,

jLabel2 = new javax.swing.JLabel(); jLabel3 = new javax.swing.JLabel(); jButton2 = new javax.swing.JButton();

setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);

jLabel1.setFont(new java.awt.Font("Lucida Sans", 1, 48)); // NOI18N jLabel1.setForeground(new java.awt.Color(255, 0, 0)); jLabel1.setText("Run@timer");

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

jSpinner1.setModel(new javax.swing.SpinnerNumberModel(Integer.valueOf(0), Integer.valueOf(0), null, Integer.valueOf(1)));

jSpinner2.setModel(new javax.swing.SpinnerNumberModel(Integer.valueOf(0), Integer.valueOf(0), null, Integer.valueOf(5)));

jLabel2.setText("minutes");

jLabel3.setText("hours");

jButton2.setFont(new java.awt.Font("Times New Roman", 1, 48)); // NOI18N jButton2.setForeground(new java.awt.Color(255, 0, 102)); jButton2.setIcon(new javax.swing.ImageIcon("C:\\Users\\swapnil dubey\\Desktop\\icon-start-play-128.png")); // NOI18N jButton2.setText("Start"); jButton2.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { jButton2ActionPerformed(evt); } });

Page | 40

Page 41: A Project Run@Timer, J2SE,

javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane()); getContentPane().setLayout(layout); layout.setHorizontalGroup( layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(layout.createSequentialGroup() .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(layout.createSequentialGroup() .addGap(130, 130, 130) .addComponent(jLabel1, javax.swing.GroupLayout.PREFERRED_SIZE, 287, javax.swing.GroupLayout.PREFERRED_SIZE)) .addGroup(layout.createSequentialGroup() .addGap(40, 40, 40) .addComponent(jTextField1, javax.swing.GroupLayout.PREFERRED_SIZE, 385, javax.swing.GroupLayout.PREFERRED_SIZE) .addGap(18, 18, 18) .addComponent(jButton1)) .addGroup(layout.createSequentialGroup() .addGap(153, 153, 153) .addComponent(jButton2, javax.swing.GroupLayout.PREFERRED_SIZE, 340, javax.swing.GroupLayout.PREFERRED_SIZE)) .addGroup(layout.createSequentialGroup() .addGap(115, 115, 115) .addComponent(jSpinner1, javax.swing.GroupLayout.PREFERRED_SIZE, 51, javax.swing.GroupLayout.PREFERRED_SIZE) .addGap(26, 26, 26) .addComponent(jLabel2) .addGap(43, 43, 43) .addComponent(jSpinner2, javax.swing.GroupLayout.PREFERRED_SIZE, 44, javax.swing.GroupLayout.PREFERRED_SIZE) .addGap(32, 32, 32) .addComponent(jLabel3))) .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)) ); layout.setVerticalGroup( layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(layout.createSequentialGroup() .addGap(46, 46, 46) .addComponent(jLabel1) .addGap(18, 18, 18)

Page | 41

Page 42: A Project Run@Timer, J2SE,

.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE) .addComponent(jTextField1, javax.swing.GroupLayout.PREFERRED_SIZE, 45, javax.swing.GroupLayout.PREFERRED_SIZE) .addComponent(jButton1, javax.swing.GroupLayout.PREFERRED_SIZE, 45, javax.swing.GroupLayout.PREFERRED_SIZE)) .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(layout.createSequentialGroup() .addGap(83, 83, 83) .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING) .addComponent(jSpinner1, javax.swing.GroupLayout.PREFERRED_SIZE, 34, javax.swing.GroupLayout.PREFERRED_SIZE) .addComponent(jLabel2))) .addGroup(layout.createSequentialGroup() .addGap(85, 85, 85) .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING) .addComponent(jLabel3) .addComponent(jSpinner2, javax.swing.GroupLayout.PREFERRED_SIZE, 33, javax.swing.GroupLayout.PREFERRED_SIZE)))) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 73, Short.MAX_VALUE) .addComponent(jButton2, javax.swing.GroupLayout.PREFERRED_SIZE, 128, javax.swing.GroupLayout.PREFERRED_SIZE) .addGap(27, 27, 27)) );

pack(); }// </editor-fold>//GEN-END:initComponentspublic void close(){ WindowEvent winClosingEvent = new WindowEvent(this,WindowEvent.WINDOW_CLOSING); Toolkit.getDefaultToolkit().getSystemEventQueue().postEvent(winClosingEvent); } private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton1ActionPerformed JFileChooser fileChooser = new JFileChooser();int returnValue = fileChooser.showOpenDialog(null);if (returnValue== JFileChooser.APPROVE_OPTION) {File file=fileChooser.getSelectedFile();

Page | 42

Page 43: A Project Run@Timer, J2SE,

jTextField1.setText(file.getAbsolutePath());ff=jTextField1.getText();} }//GEN-LAST:event_jButton1ActionPerformed

private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton2ActionPerformed if("".equals(jTextField1.getText())) {JOptionPane.showMessageDialog(this, "Select a file or process first", "Warning", JOptionPane.ERROR_MESSAGE); } else{ close(); int mins=Integer.valueOf(jSpinner1.getValue().toString()); int hours=Integer.valueOf(jSpinner2.getValue().toString()); int tot=(hours*60)+mins; if(tot==0)JOptionPane.showMessageDialog(this, "Timer can't be Zero", "Warning", JOptionPane.ERROR_MESSAGE); else { seconds=60*tot; SecondFrame form= new SecondFrame(); form.setLocationRelativeTo(null); form.setVisible(true); } } }//GEN-LAST:event_jButton2ActionPerformed

/** * @param args the command line arguments */

static String ff;static int seconds;

public static void main(String args[]) { /* Set the Nimbus look and feel */ //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) "> /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.

Page | 43

Page 44: A Project Run@Timer, J2SE,

* For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html */ try { for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) { if ("Nimbus".equals(info.getName())) { javax.swing.UIManager.setLookAndFeel(info.getClassName()); break; } } } catch (ClassNotFoundException ex) { java.util.logging.Logger.getLogger(FirstFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex); } catch (InstantiationException ex) { java.util.logging.Logger.getLogger(FirstFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex); } catch (IllegalAccessException ex) { java.util.logging.Logger.getLogger(FirstFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex); } catch (javax.swing.UnsupportedLookAndFeelException ex) { java.util.logging.Logger.getLogger(FirstFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex); } //</editor-fold>

/* Create and display the form */ java.awt.EventQueue.invokeLater(new Runnable() { public void run() { new FirstFrame().setVisible(true); } }); }

// Variables declaration - do not modify//GEN-BEGIN:variables private javax.swing.JButton jButton1; private javax.swing.JButton jButton2; private javax.swing.JLabel jLabel1; private javax.swing.JLabel jLabel2; private javax.swing.JLabel jLabel3; private javax.swing.JSpinner jSpinner1; private javax.swing.JSpinner jSpinner2; private javax.swing.JTextField jTextField1;

Page | 44

Page 45: A Project Run@Timer, J2SE,

// End of variables declaration//GEN-END:variables}

Detailed coding for the second Frame:

/* * To change this license header, choose License Headers in Project Properties. * To change this template file, choose Tools | Templates * and open the template in the editor.

Page | 45

Page 46: A Project Run@Timer, J2SE,

*/import java.awt.Desktop;import java.io.File;import java.io.IOException;import java.util.Timer;import java.util.TimerTask;

/** * * @author swapnil dubey */public class SecondFrame extends javax.swing.JFrame {

/** * Creates new form SecondFrame */ public SecondFrame() { initComponents(); startime();} TimerTask task = new SecondFrame.RunMeTask1(); Timer timer = new Timer(); public void startime() { jTextField1.setText(s); timer.schedule(task, 0,1000); }

/** * 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") // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents private void initComponents() {

jLabel1 = new javax.swing.JLabel(); jScrollPane1 = new javax.swing.JScrollPane(); jTextField1 = new javax.swing.JTextField(); jLabel2 = new javax.swing.JLabel(); jButton1 = new javax.swing.JButton();

setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

Page | 46

Page 47: A Project Run@Timer, J2SE,

jLabel1.setFont(new java.awt.Font("Tahoma", 1, 36)); // NOI18N jLabel1.setText("To Run");

jScrollPane1.setViewportView(jTextField1);

jButton1.setFont(new java.awt.Font("Tahoma", 1, 48)); // NOI18N jButton1.setForeground(new java.awt.Color(255, 0, 102)); jButton1.setIcon(new javax.swing.ImageIcon("C:\\Users\\swapnil dubey\\Desktop\\cancel_red.png")); // NOI18N jButton1.setText("Stop"); jButton1.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { jButton1ActionPerformed(evt); } });

javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane()); getContentPane().setLayout(layout); layout.setHorizontalGroup( layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(layout.createSequentialGroup() .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(layout.createSequentialGroup() .addContainerGap() .addComponent(jLabel1) .addGap(32, 32, 32) .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 363, javax.swing.GroupLayout.PREFERRED_SIZE)) .addGroup(layout.createSequentialGroup() .addGap(71, 71, 71) .addComponent(jLabel2, javax.swing.GroupLayout.PREFERRED_SIZE, 385, javax.swing.GroupLayout.PREFERRED_SIZE)) .addGroup(layout.createSequentialGroup() .addGap(166, 166, 166) .addComponent(jButton1, javax.swing.GroupLayout.PREFERRED_SIZE, 301, javax.swing.GroupLayout.PREFERRED_SIZE))) .addContainerGap(44, Short.MAX_VALUE)) );

Page | 47

Page 48: A Project Run@Timer, J2SE,

layout.setVerticalGroup( layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(layout.createSequentialGroup() .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(layout.createSequentialGroup() .addGap(73, 73, 73) .addComponent(jLabel1)) .addGroup(layout.createSequentialGroup() .addGap(50, 50, 50) .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 100, javax.swing.GroupLayout.PREFERRED_SIZE))) .addGap(74, 74, 74) .addComponent(jLabel2, javax.swing.GroupLayout.PREFERRED_SIZE, 61, javax.swing.GroupLayout.PREFERRED_SIZE) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 47, Short.MAX_VALUE) .addComponent(jButton1) .addGap(35, 35, 35)) );

pack(); }// </editor-fold>//GEN-END:initComponentsFirstFrame steg = new FirstFrame(); String s=steg.ff; private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton1ActionPerformed timer.cancel();jLabel2.setText("Stopped"); }//GEN-LAST:event_jButton1ActionPerformedpublic class RunMeTask1 extends TimerTask{ int seconds=steg.seconds; public void run() { if (seconds > 0) { int r=seconds/60; int rem=seconds%60; int hr=r/60; int rr=r%60; if(r==0) { jLabel2.setText(seconds + " secs Remaining"); seconds--;

Page | 48

Page 49: A Project Run@Timer, J2SE,

} else if(hr==0) { jLabel2.setText(r+" Mins and "+rem+" secs Remaining"); seconds--; } else { jLabel2.setText(hr+" hours "+rr+" Mins and "+rem+" secs Remaining"); seconds--; } } else if(seconds==-1) { return; } else if(seconds==0) { try{ File f = new File(s); Desktop dt = Desktop.getDesktop(); dt.open(f); }catch(IOException e){ e.printStackTrace(); } jLabel2.setText(" Successfully Executed "); seconds=-1; //System.exit(0); } }} /** * @param args the command line arguments */ public static void main(String args[]) { /* Set the Nimbus look and feel */ //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) "> /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel. * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html */

Page | 49

Page 50: A Project Run@Timer, J2SE,

try { for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) { if ("Nimbus".equals(info.getName())) { javax.swing.UIManager.setLookAndFeel(info.getClassName()); break; } } } catch (ClassNotFoundException ex) { java.util.logging.Logger.getLogger(SecondFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex); } catch (InstantiationException ex) { java.util.logging.Logger.getLogger(SecondFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex); } catch (IllegalAccessException ex) { java.util.logging.Logger.getLogger(SecondFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex); } catch (javax.swing.UnsupportedLookAndFeelException ex) { java.util.logging.Logger.getLogger(SecondFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex); } //</editor-fold>

/* Create and display the form */ java.awt.EventQueue.invokeLater(new Runnable() { public void run() { new SecondFrame().setVisible(true); } }); }

// Variables declaration - do not modify//GEN-BEGIN:variables private javax.swing.JButton jButton1; private javax.swing.JLabel jLabel1; private javax.swing.JLabel jLabel2; private javax.swing.JScrollPane jScrollPane1; private javax.swing.JTextField jTextField1; // End of variables declaration//GEN-END:variables}

Page | 50

Page 51: A Project Run@Timer, J2SE,

Detailed coding for main class :

/* * To change this license header, choose License Headers in Project Properties. * To change this template file, choose Tools | Templates * and open the template in the editor. */

Page | 51

Page 52: A Project Run@Timer, J2SE,

/** * * @author swapnil dubey */public class main {

/** * @param args the command line arguments */ public static void main(String[] args) { FirstFrame form=new FirstFrame (); form.setLocationRelativeTo(null); form.setVisible(true); } }

Conclusion:

Applications like this should be developed more frequently so that more amount of work can be done in a short span of time and not worrying anything about the upcoming tasks.

Page | 52

Page 53: A Project Run@Timer, J2SE,

Refrences:

www.google.comwww.wickepedia.orgwww.examples.codegeeks.com/core-javawww.javarevisited.blogspot.inwww.mkyong.com/java

Page | 53