0

so this is my update code where i have issue in formatting Timer in Java GUI..

import java.awt.Color;
import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
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 JPanel contentPane;
    Timer tm;
    Timer tm2;
    int i = 0;
    int o = 0;

    public deploy() {

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

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

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

            @Override
            public void actionPerformed(ActionEvent e) {
                lblTimer2.setText(Integer.toString(o));
                o++;
            }
        });

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

                tm2.start();
            }
        });
        btnNewButton.setBounds(289, 257, 89, 32);
        contentPane.add(btnNewButton);

        JButton btnNewButton_1 = new JButton("Stop");
        btnNewButton_1.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {

                tm2.stop();
            }
        });
        pack();
        setVisible(true);
    }

    public static void main(String[] args) {
        new deploy();
    }
}

this is the part in my code that i need your help guys..i want my lblTimer2 will display the format of "00:00". but my code i did here is formatted as "0" and so on.. because i'm creating a GUI cybercafe management software which the feature of my GUI is to time the client and after the client finish with his/her work the time will stop and it will compute the time he/she spent that it will go through billing transaction. i'm new in Programming and using Eclipse Neon for Java GUI Swing Application.

mKorbel
  • 109,525
  • 20
  • 134
  • 319
Dev_Gab
  • 25
  • 8
  • 1
    Welcome to SO. Please post [MCVE] – c0der Mar 18 '17 at 07:53
  • [Thats one way to do it](http://stackoverflow.com/questions/33487186/swing-timer-stopwatch-in-java/33488613#33488613) – MadProgrammer Mar 18 '17 at 08:44
  • [Thats another way to do it](http://stackoverflow.com/questions/21011914/creating-a-gui-based-chronometer-or-stopwatch/21012092#21012092) – MadProgrammer Mar 18 '17 at 08:45
  • [A different way](http://stackoverflow.com/questions/32961391/java-finding-difference-between-times/32961667#32961667) – MadProgrammer Mar 18 '17 at 08:47
  • @c0der hello i updated my post.. i hope it will clarify now.. i'm new in programming and stack overflow.. thank you for your reply... – Dev_Gab Mar 18 '17 at 09:01
  • Are you referring to this question ? http://stackoverflow.com/questions/42902512/how-to-reset-a-timer-in-java-gui-and-display-message-after-stop – c0der Mar 23 '17 at 05:20

2 Answers2

-1

Try format to format time in the required form.

Date d = new Date(o * 1000L);
SimpleDateFormat df = new SimpleDateFormat("HH:mm:ss"); // HH for 0-23
df.setTimeZone(TimeZone.getTimeZone("GMT"));
String time = df.format(d);
o++;
  • This will help you, check now – SANTOSHKUMAR SINGH Mar 18 '17 at 07:49
  • Thank you SANTOSHKUMARSINGH it works! thanks to @c0der for properly place the code you were given.. – Dev_Gab Mar 18 '17 at 14:14
  • I already accept your answer. i'm new in Stack Overflow. i'm starting to familiarize this site.. anyways thank u so much – Dev_Gab Mar 18 '17 at 15:08
  • I see that you "un accepted" this question and accepted mine. @SANTOSHKUMARSINGH is correct and helpful. Please note that you can also up-vote answers. – c0der Mar 21 '17 at 08:30
  • hello @c0der i have no privilege yet to up-vote his answer. though it is helpful. but for me the best solution is yours so i accepted. if i have a privilege to up-vote answers i will up-vote also this. – Dev_Gab Mar 23 '17 at 01:32
-1

Please see comments :

public class deploy extends JFrame {

    private int seconds;
    private SimpleDateFormat df;
    private boolean isRunning;
    private JLabel lblTimer2;

    public deploy() {

        JPanel contentPane = new JPanel();
        contentPane.setBackground(Color.DARK_GRAY);
        contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
        setContentPane(contentPane);
        //better avoid null layout managers
        //contentPane.setLayout(null);
        contentPane.setLayout(new BorderLayout());

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

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

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

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

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

                isRunning = !isRunning;
            }
        });
        //btnNewButton.setBounds(289, 257, 89, 32);
        btnNewButton.setPreferredSize(new Dimension(100,30));
        contentPane.add(btnNewButton, BorderLayout.SOUTH);

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

        seconds = 0;
        isRunning = false;
        setTimer();
        pack();
        setVisible(true);
    }


    private void setTimer() {

        //based on SANTOSHKUMAR SINGH answer
        Date d = new Date(seconds * 1000L);
        String time = df.format(d);
        lblTimer2.setText(time);
    }

    public static void main(String[] args) {
        new deploy();
    }
}
c0der
  • 18,467
  • 6
  • 33
  • 65
  • omg it works! thank you so much! @c0der but something happens in my design tab where i can drag n drop the components of a frame. it tried to resize but it doesn't work until i run, it works but after i stop run i can't edit my other labels and buttons and even move it. is there any happen in JPanel? – Dev_Gab Mar 18 '17 at 14:12
  • hello @c0der i have problems in resetting the timer. i need your help.. i create a button where i can reset the timer and use tm2.restart(); but its not working... – Dev_Gab Mar 23 '17 at 01:36