Results 1 to 2 of 2
  1. #1
    Join Date
    Jun 2012
    Posts
    7
    Rep Power
    0

    Lightbulb Help with a stop and continue button with a timer plz. JAVA

    hey guys... I need some help with this timer. I want a stop button to pause the time and the when i click continue it will just continue where it stopped. When it hits 00:00 I want to click te reset button so that it resets the time to 35 and when I click start , it starts counting back again. It works at the moment without the , Stop, Continue and reset button. I'm realy new to java so not sure how to handle this please help.

    This is my code. Done in Netbeanz

    */
    import java.awt.*;
    import java.awt.event.*;
    import java.util.List;
    import java.util.concurrent.TimeUnit;
    import javax.security.auth.callback.Callback;
    import javax.swing.*;
    public class Countdown extends javax.swing.JFrame {
    private javax.swing.JLabel jLblTimer;
    private javax.swing.JButton jBtnStart;

    /** Creates new form Countdown */
    public Countdown() {
    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() {

    jPanel1 = new javax.swing.JPanel();
    jButton1 = new javax.swing.JButton();
    jLabel1 = new javax.swing.JLabel();
    jTextField3 = new javax.swing.JTextField();
    jTextField6 = new javax.swing.JTextField();
    jTextField4 = new javax.swing.JTextField();
    jTextField5 = new javax.swing.JTextField();
    jTextField7 = new javax.swing.JTextField();
    jButton2 = new javax.swing.JButton();
    jButton3 = new javax.swing.JButton();
    jButton4 = new javax.swing.JButton();

    setDefaultCloseOperation(javax.swing.WindowConstan ts.EXIT_ON_CLOSE);

    jPanel1.setBackground(new java.awt.Color(0, 51, 255));

    jButton1.setBackground(new java.awt.Color(0, 51, 255));
    jButton1.setText("Start");
    jButton1.addActionListener(new java.awt.event.ActionListener() {
    public void actionPerformed(java.awt.event.ActionEvent evt) {
    jButton1ActionPerformed(evt);
    }
    });

    jLabel1.setBackground(new java.awt.Color(51, 255, 51));
    jLabel1.setFont(new java.awt.Font("Calibri", 1, 250));
    jLabel1.setForeground(new java.awt.Color(255, 255, 255));
    jLabel1.setText("35:00");

    jTextField3.setBackground(new java.awt.Color(0, 51, 255));
    jTextField3.setFont(new java.awt.Font("Calibri", 1, 130));
    jTextField3.setForeground(new java.awt.Color(255, 255, 255));
    jTextField3.addActionListener(new java.awt.event.ActionListener() {
    public void actionPerformed(java.awt.event.ActionEvent evt) {
    jTextField3ActionPerformed(evt);
    }
    });

    jTextField6.setBackground(new java.awt.Color(0, 51, 255));
    jTextField6.setFont(new java.awt.Font("Calibri", 1, 130));
    jTextField6.setForeground(new java.awt.Color(255, 255, 255));
    jTextField6.addActionListener(new java.awt.event.ActionListener() {
    public void actionPerformed(java.awt.event.ActionEvent evt) {
    jTextField6ActionPerformed(evt);
    }
    });

    jTextField4.setBackground(new java.awt.Color(0, 51, 255));
    jTextField4.setFont(new java.awt.Font("Calibri", 1, 130));
    jTextField4.setForeground(new java.awt.Color(255, 255, 255));
    jTextField4.addActionListener(new java.awt.event.ActionListener() {
    public void actionPerformed(java.awt.event.ActionEvent evt) {
    jTextField4ActionPerformed(evt);
    }
    });

    jTextField5.setBackground(new java.awt.Color(0, 51, 255));
    jTextField5.setFont(new java.awt.Font("Calibri", 1, 130));
    jTextField5.setForeground(new java.awt.Color(255, 255, 255));
    jTextField5.addActionListener(new java.awt.event.ActionListener() {
    public void actionPerformed(java.awt.event.ActionEvent evt) {
    jTextField5ActionPerformed(evt);
    }
    });

    jTextField7.setBackground(new java.awt.Color(0, 51, 255));
    jTextField7.setFont(new java.awt.Font("Calibri", 1, 100));
    jTextField7.setForeground(new java.awt.Color(255, 255, 255));
    jTextField7.addActionListener(new java.awt.event.ActionListener() {
    public void actionPerformed(java.awt.event.ActionEvent evt) {
    jTextField7ActionPerformed(evt);
    }
    });

    jButton2.setText("Stop");

    jButton3.setText("Continue");

    jButton4.setText("reset");

    javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
    jPanel1.setLayout(jPanel1Layout);
    jPanel1Layout.setHorizontalGroup(
    jPanel1Layout.createParallelGroup(javax.swing.Grou pLayout.Alignment.LEADING)
    .addGroup(jPanel1Layout.createSequentialGroup()
    .addContainerGap()
    .addGroup(jPanel1Layout.createParallelGroup(javax. swing.GroupLayout.Alignment.LEADING)
    .addGroup(jPanel1Layout.createSequentialGroup()
    .addGroup(jPanel1Layout.createParallelGroup(javax. swing.GroupLayout.Alignment.TRAILING)
    .addComponent(jTextField6, javax.swing.GroupLayout.PREFERRED_SIZE, 911, javax.swing.GroupLayout.PREFERRED_SIZE)
    .addComponent(jTextField3, javax.swing.GroupLayout.DEFAULT_SIZE, 911, Short.MAX_VALUE))
    .addGap(507, 507, 507))
    .addComponent(jButton1))
    .addPreferredGap(javax.swing.LayoutStyle.Component Placement.RELATED)
    .addGroup(jPanel1Layout.createParallelGroup(javax. swing.GroupLayout.Alignment.LEADING, false)
    .addComponent(jTextField4)
    .addComponent(jTextField5, javax.swing.GroupLayout.DEFAULT_SIZE, 225, Short.MAX_VALUE))
    .addGap(2599, 2599, 2599))
    .addGroup(javax.swing.GroupLayout.Alignment.TRAILI NG, jPanel1Layout.createSequentialGroup()
    .addContainerGap()
    .addGroup(jPanel1Layout.createParallelGroup(javax. swing.GroupLayout.Alignment.LEADING)
    .addComponent(jButton2)
    .addComponent(jButton3)
    .addComponent(jButton4))
    .addPreferredGap(javax.swing.LayoutStyle.Component Placement.RELATED, 674, Short.MAX_VALUE)
    .addComponent(jLabel1, javax.swing.GroupLayout.PREFERRED_SIZE, 861, javax.swing.GroupLayout.PREFERRED_SIZE)
    .addGap(2632, 2632, 2632))
    .addGroup(jPanel1Layout.createSequentialGroup()
    .addGap(661, 661, 661)
    .addComponent(jTextField7, javax.swing.GroupLayout.DEFAULT_SIZE, 911, Short.MAX_VALUE)
    .addGap(2680, 2680, 2680))
    );
    jPanel1Layout.setVerticalGroup(
    jPanel1Layout.createParallelGroup(javax.swing.Grou pLayout.Alignment.LEADING)
    .addGroup(jPanel1Layout.createSequentialGroup()
    .addContainerGap()
    .addComponent(jTextField7, javax.swing.GroupLayout.PREFERRED_SIZE, 150, javax.swing.GroupLayout.PREFERRED_SIZE)
    .addPreferredGap(javax.swing.LayoutStyle.Component Placement.RELATED)
    .addGroup(jPanel1Layout.createParallelGroup(javax. swing.GroupLayout.Alignment.LEADING)
    .addGroup(jPanel1Layout.createSequentialGroup()
    .addGap(50, 50, 50)
    .addGroup(jPanel1Layout.createParallelGroup(javax. swing.GroupLayout.Alignment.BASELINE)
    .addComponent(jTextField3, javax.swing.GroupLayout.PREFERRED_SIZE, 150, javax.swing.GroupLayout.PREFERRED_SIZE)
    .addComponent(jTextField4, javax.swing.GroupLayout.PREFERRED_SIZE, 150, javax.swing.GroupLayout.PREFERRED_SIZE))
    .addGap(75, 75, 75)
    .addGroup(jPanel1Layout.createParallelGroup(javax. swing.GroupLayout.Alignment.LEADING)
    .addComponent(jTextField5, javax.swing.GroupLayout.PREFERRED_SIZE, 150, javax.swing.GroupLayout.PREFERRED_SIZE)
    .addGroup(jPanel1Layout.createSequentialGroup()
    .addComponent(jTextField6, javax.swing.GroupLayout.PREFERRED_SIZE, 160, javax.swing.GroupLayout.PREFERRED_SIZE)
    .addPreferredGap(javax.swing.LayoutStyle.Component Placement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
    .addComponent(jButton1)
    .addGap(18, 18, 18)
    .addComponent(jButton2)
    .addGap(18, 18, 18)
    .addComponent(jButton3)
    .addGap(18, 18, 18)
    .addComponent(jButton4)
    .addGap(81, 81, 81)))
    .addGap(350, 350, 350))
    .addGroup(jPanel1Layout.createSequentialGroup()
    .addPreferredGap(javax.swing.LayoutStyle.Component Placement.RELATED, 400, Short.MAX_VALUE)
    .addComponent(jLabel1, javax.swing.GroupLayout.PREFERRED_SIZE, 350, javax.swing.GroupLayout.PREFERRED_SIZE)
    .addGap(268, 268, 268))))
    );

    javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
    getContentPane().setLayout(layout);
    layout.setHorizontalGroup(
    layout.createParallelGroup(javax.swing.GroupLayout .Alignment.LEADING)
    .addGroup(javax.swing.GroupLayout.Alignment.TRAILI NG, layout.createSequentialGroup()
    .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
    .addContainerGap(132, Short.MAX_VALUE))
    );
    layout.setVerticalGroup(
    layout.createParallelGroup(javax.swing.GroupLayout .Alignment.LEADING)
    .addGroup(javax.swing.GroupLayout.Alignment.TRAILI NG, layout.createSequentialGroup()
    .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
    .addContainerGap(102, Short.MAX_VALUE))
    );

    pack();
    }// </editor-fold>//GEN-END:initComponents

    private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton1ActionPerformed
    for(int ss=0; ss<1; ss++) {
    // Change the set name

    // Setup the counter to begin the countdown
    counter = 35*60;
    timer = new Timer(1000, startCycle());
    timer.start();
    }


    }
    public static void main(String args[]) {
    java.awt.EventQueue.invokeLater(new Runnable() {
    public void run() {
    new Countdown().setVisible(true);
    }
    });
    }
    private Action startCycle() {
    return new AbstractAction() {
    @Override
    public void actionPerformed(ActionEvent e) {
    new ClockTask(null).execute();
    }
    };
    }
    class ClockTask extends SwingWorker<Void, String> {
    private Callback callback;
    private Object jLblTimer;
    public ClockTask(Callback callback) {
    this.callback = callback;
    }
    @Override
    protected Void doInBackground() throws Exception {
    if (counter >= 0) {
    int millis = counter*1000;
    String time = String.format("%02d:%02d", TimeUnit.MILLISECONDS.toMinutes(millis),
    TimeUnit.MILLISECONDS.toSeconds(millis) -
    TimeUnit.MINUTES.toSeconds(TimeUnit.MILLISECONDS.t oMinutes(millis))
    );
    publish(time); // call publish which will call process() method in EDT
    counter--;
    } else {
    // this.callback.call();
    this.callback.notify();
    timer.stop();
    }
    return(null);
    }

    @Override
    protected void process(List<String> times) {
    String lastTime = times.get(times.size()-1);
    jLabel1.setText(lastTime); // just update ui with lastTimer, ohters are ignorable
    }
    @Override
    protected void done() {
    super.done();
    } // TODO add your handling code here:
    }//GEN-LAST:event_jButton1ActionPerformed

    private void jTextField3ActionPerformed(java.awt.event.ActionEv ent evt) {//GEN-FIRST:event_jTextField3ActionPerformed
    // TODO add your handling code here:
    }//GEN-LAST:event_jTextField3ActionPerformed

    private void jTextField6ActionPerformed(java.awt.event.ActionEv ent evt) {//GEN-FIRST:event_jTextField6ActionPerformed
    // TODO add your handling code here:
    }//GEN-LAST:event_jTextField6ActionPerformed

    private void jTextField4ActionPerformed(java.awt.event.ActionEv ent evt) {//GEN-FIRST:event_jTextField4ActionPerformed
    // TODO add your handling code here:
    }//GEN-LAST:event_jTextField4ActionPerformed

    private void jTextField5ActionPerformed(java.awt.event.ActionEv ent evt) {//GEN-FIRST:event_jTextField5ActionPerformed
    // TODO add your handling code here:
    }//GEN-LAST:event_jTextField5ActionPerformed

    private void jTextField7ActionPerformed(java.awt.event.ActionEv ent evt) {//GEN-FIRST:event_jTextField7ActionPerformed
    // TODO add your handling code here:
    }//GEN-LAST:event_jTextField7ActionPerformed

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

    private javax.swing.Timer timer;
    private int counter;
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton jButton1;
    private javax.swing.JButton jButton2;
    private javax.swing.JButton jButton3;
    private javax.swing.JButton jButton4;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JTextField jTextField3;
    private javax.swing.JTextField jTextField4;
    private javax.swing.JTextField jTextField5;
    private javax.swing.JTextField jTextField6;
    private javax.swing.JTextField jTextField7;
    // End of variables declaration//GEN-END:variables

    }

  2. #2
    DarrylBurke's Avatar
    DarrylBurke is offline Member
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,197
    Rep Power
    19

    Default Re: Help with a stop and continue button with a timer plz. JAVA

    Not an advanced question at all. Moving to AWT/Swing.

    Please go through these:
    Forum Rules
    Guide For New Members
    BB Code List - Java Programming Forum

    Also, to get better help sooner, post a SSCCE (Short, Self Contained, Correct (Compilable), Example) that demonstrates the problem. Forum members aren't too likely to plough their way through reams of unrelated IDE-generated gobbledygook to find where the Timer issue is. Have you gone through the API for the class?

    db
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

Similar Threads

  1. START/STOP Button
    By tnrh1 in forum New To Java
    Replies: 13
    Last Post: 08-18-2011, 02:40 PM
  2. Stop a timer in other class
    By warchieflll in forum Advanced Java
    Replies: 15
    Last Post: 02-02-2011, 08:13 PM
  3. Stopping a thread using a stop button - GUI
    By Ben1 in forum New To Java
    Replies: 5
    Last Post: 01-27-2011, 04:21 PM
  4. Use stop button to stop moving (stop timers) on JPanel
    By mneskovic in forum New To Java
    Replies: 3
    Last Post: 07-23-2010, 12:50 PM
  5. stop button in the browser
    By Peter in forum Java Servlet
    Replies: 2
    Last Post: 07-04-2007, 07:21 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •