1

Hello Good day i want to create a "Reset Button" where my Timer will reset. I created a new button and named it as "reset" and i use the code "tm2.restart();" but its not working in my created New Button. This is my code:

import javax.swing.Timer;
public class deploy extends JFrame {

 private int seconds;
 private SimpleDateFormat df;
 private boolean isRunning;
 private JLabel lblTimer1;
 private JButton btnStart1;

public deploy() {

lblTimer1 = new JLabel("New label");
lblTimer1.setForeground(Color.WHITE);
lblTimer1.setFont(new Font("Tahoma", Font.PLAIN, 20));
lblTimer1.setBounds(100, 231, 94, 16);
contentPane.add(lblTimer1);

 Timer tm2 = new Timer(1000, new ActionListener() {

           @Override
            public void actionPerformed(ActionEvent e) {
                setTimer();
                seconds++;
            }
        });

btnStart1 = new JButton("Start");
btnStart1.setBackground(Color.LIGHT_GRAY);
btnStart1.setForeground(Color.BLUE);
btnStart1.addActionListener(new ActionListener() {
     @Override
           public void actionPerformed(ActionEvent e) {

                if(isRunning) {
                    tm2.stop();
                    btnStart1.setText("Start");
                }else {
                    tm2.start();
                    btnStart1.setText("Stop");
                }

                isRunning = !isRunning;
            }
        });

My kind of timer is formatted as "SimpleDateFormat" (00:00:00) because i'm creating a "cybercafe management application" that the customer will walk in and records his/her time until he/she logged out and display a message the amount he/she will be payed off. please help. thanks

Dev_Gab
  • 25
  • 8
  • 1
    I don't see `tm2.restart()` in that code ... please provide a [mcve]. – Fildor Mar 20 '17 at 11:42
  • 1
    BTW, tracking a timespan with a Timer is not really the best of ideas ... You can simply record 2 timestamps (at login, at logout) and calculate the diff, when needed. – Fildor Mar 20 '17 at 11:45
  • @Fildor i don't include the code in the format tm2.restart(); i delete because since it's not working.. – Dev_Gab Mar 20 '17 at 12:51
  • ah okay. what code should i use then? i'm new in Java GUI... – Dev_Gab Mar 20 '17 at 12:52
  • You can always calculate the actual timespan between now and some time in the past. There are different ways to achieve a secondly update on a label showing the elapsed time. – Fildor Mar 20 '17 at 12:58
  • For example: http://stackoverflow.com/a/26699423/982149 - just instead of showing current time, calculate difference between current and login. (Or just "00:00" if no loginTime is set.) On logout, take difference between logoutTime and loginTime to calculate the time to charge the customer. You can make extensive use of the new DateTime API: http://www.oracle.com/technetwork/articles/java/jf14-date-time-2125367.html – Fildor Mar 20 '17 at 13:00
  • Please post a [MCVE] including the needed imports. This code does not compile: `contentPane.add(lblTimer1);` ? `setTimer();` ? – c0der Mar 23 '17 at 05:26

1 Answers1

1

Review code and don't hesitate to ask what is unclear:

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.TimeZone;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.Timer;
import javax.swing.border.EmptyBorder;
public class Deploy extends JFrame {

    private int seconds;
    private SimpleDateFormat df;
    private JLabel lblTimer;
    private Timer timer;
    private  JButton startButton;

    public Deploy() {

        JPanel contentPane = new JPanel();
        contentPane.setBackground(Color.DARK_GRAY);
        contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
        setContentPane(contentPane);
        contentPane.setLayout(new BorderLayout());

        lblTimer = new JLabel();
        lblTimer.setForeground(Color.WHITE);
        lblTimer.setFont(new Font("Tahoma", Font.PLAIN, 20));
        lblTimer.setPreferredSize(new Dimension(100,30));
        contentPane.add(lblTimer,BorderLayout.NORTH);

        timer = new Timer(1000, new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                setTimer();
                seconds++;
            }
        });

        JPanel buttonsPanel = new JPanel();
        contentPane.add(buttonsPanel, BorderLayout.SOUTH);

        startButton = new JButton("Start");
        startButton.setBackground(Color.LIGHT_GRAY);
        startButton.setForeground(Color.BLUE);
        startButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {

                if(timer.isRunning()) {
                    timer.stop();
                    startButton.setText("Start");
                }else {
                    timer.start();
                    startButton.setText("Stop");
                }
            }
        });

        startButton.setPreferredSize(new Dimension(100,30));
        buttonsPanel.add(startButton);

        JButton resetButton = new JButton("Reset");
        resetButton.setBackground(Color.LIGHT_GRAY);
        resetButton.setForeground(Color.RED);
        resetButton.addActionListener(new ActionListener() {

            @Override
            public void actionPerformed(ActionEvent e) {
                resetTimer();
            }
        });
        resetButton.setPreferredSize(new Dimension(100,30));
        buttonsPanel.add(resetButton);

        df = new SimpleDateFormat("HH:mm:ss"); // HH for 0-23
        df.setTimeZone(TimeZone.getTimeZone("GMT"));

        resetTimer();
        pack();
        setVisible(true);
    }

    private void setTimer() {
        Date d = new Date(seconds * 1000L);
        String time = df.format(d);
        lblTimer.setText(time);
    }

    private void resetTimer() {

        if(timer.isRunning()) {
            timer.stop();
        }
        startButton.setText("Start");
        seconds = 0;
        setTimer();
    }

    public static void main(String[] args) {
        new Deploy();
    }
}
c0der
  • 18,467
  • 6
  • 33
  • 65