user2999509
user2999509

Reputation: 107

How to put timer into a GUI?

I have a GUI with a form for people to fill up and I would like to put a countdown timer at the top right hand corner of the page

Heres the method for the timer to get the remaining time. Say my form class is FillForm and the timer method is found in Timer.

How do I put a dynamic (constantly updating) timer inside of the GUI?

public String getRemainingTime() {
    int hours = (int)((this.remainingTime/3600000) % 60);
    int minutes = (int)((this.remainingTime/60000) % 60);
    int seconds = (int)(((this.remainingTime)/1000) % 60);

    return(format.format(hours) +  ":" +  format.format(minutes)+ 
          ":" +  format.format(seconds));       
}

GUI is built using NetBeans GUI builder.

Upvotes: 1

Views: 25720

Answers (3)

Paul Samsotha
Paul Samsotha

Reputation: 209132

"Could i add this into a Swing.JPanel or something?"

Just put it in the constructor of your form class. Declare the Timer timer; as a class member and not locally scoped so that you can use the start() method like in a button's actionPerformed. Something like

import javax.swing.Timer;

public class GUI extends JFrame {

    public Timer timer = null;

    public GUI() {
        timer = new Timer (500, new ActionListener(){
            public void actionPerformed(ActionEvent e) {
                if (timerGetsToZero) {
                    ((Timer)e.getSource()).stop();
                } else {
                    timeLabel.setText(getRemainingTime());
                }
            }
        });
    }

    private void startButtonActionPerformed(ActionEvent e) {
        timer.start();
    }
}

Upvotes: 1

Erwin Bolwidt
Erwin Bolwidt

Reputation: 31299

You should abstract your timer into a UI component. JLabel seems the most suited as it is a text that you want to display.

public class TimerLabel extends JLabel {
    // Add in your code for 'format' and 'remainingTime'.
    // Note that the first time that 'getText' is called, it's called from the constructor
    // if the superclass, so your own class is not fully initialized at this point.
    // Hence the 'if (format != null)' check

    public TimerLabel() {
        Timer timer = new Timer(1000, new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                repaint();
            }
        });
        timer.start();
    }

    public String getRemainingTime() {
        int hours = (int) ((this.remainingTime / 3600000) % 60);
        int minutes = (int) ((this.remainingTime / 60000) % 60);
        int seconds = (int) (((this.remainingTime) / 1000) % 60);

        return (format.format(hours) + ":" + format.format(minutes) + ":" + format.format(seconds));
    }

    @Override
    public String getText() {
        if (format != null) {
            return getRemainingTime();
        } else {
            return "";
        }
    }

Upvotes: 1

Benjamin
Benjamin

Reputation: 2296

Try This :

      import javax.swing.Timer; 
      Timer  timer=new Timer(1000,new ActionListener(){
         public void actionPerformed(ActionEvent e)
     {
        //code here 
     }
     });
     timer.start();
    //timer.stop()

Every one Seconds Timer Execute.

   Try This Demo :

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JFrame;
import javax.swing.Timer;

class Counter {

private static int cnt;
static JFrame f;
public static void main(String args[]) {

 f=new JFrame();
 f.setSize(100,100);
 f.setVisible(true);


 ActionListener actListner = new ActionListener() {

 @Override

 public void actionPerformed(ActionEvent event) {
  cnt += 1;
 if(cnt%2==0)
 {
    f.setVisible(true);
 }
 else
 {
    f.setVisible(false);
 }
 }

};

Timer timer = new Timer(500, actListner);

 timer.start();
  }
 }

Upvotes: 1

Related Questions