1

below in the sample code which i wrote where the user gets an interface with 2 buttons. when the user click on start button the timer starts and when the end button is clicked the timer stops and the difference in time is displayed. But the difference in time is not being output:(

can someone help mi.

import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JTextField;

public class Timer2 extends JFrame {

    private JButton start;
    private JButton end;

    public Timer2() {
        super("Test Timer");
        setLayout(new FlowLayout());
        start = new JButton("START");
        add(start);
        end = new JButton("END");
        add(end);

        ButtonHandler handler = new ButtonHandler();
        start.addActionListener(handler);
        end.addActionListener(handler);
    }

    private class ButtonHandler implements ActionListener {

        public void actionPerformed(ActionEvent event) {
            long s_time = 0;
            long e_time = 0;
            long diff = 0;
            String name = ((JButton) event.getSource()).getText();
            if (name.equals("start")) {
                s_time = System.currentTimeMillis();
            } else {
                e_time = System.currentTimeMillis();
            }
            diff = (e_time - s_time) / 1000;
            JOptionPane.showMessageDialog(null, diff);
        }
    }

    public static void main(String[] args) {
        Timer2 timer2 = new Timer2();
        timer2.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        timer2.setSize(200, 200);
        timer2.setVisible(true);
    }
}
Alex K
  • 22,315
  • 19
  • 108
  • 236
Ravi77
  • 87
  • 2
  • 3
  • 12

2 Answers2

4

You've given your buttons text in UPPER CASE but then are looking for lower case in your event handler.

You also are setting both s_time and e_time to 0 inside the actionPerformed() method which means they are set to 0 every time you click. These both need to be fields in the ButtonHandler class.

In addition, the way you have it written, the JOptionPane.showMessageDialog() will be fired when you click either button.

Edit: To solve the last problem, move your diff calculation and JOptionPane.showMessageDialog() call to inside the else block it follows; you only want it when the "end" button is pressed.

Brian Roach
  • 76,169
  • 12
  • 136
  • 161
2

I'm assuming you want to display the result only when the user clicks "End". The way you've written it, the dialog will be displayed either way. To resolve this, move the JOptionPane.showMessageDialog(null, diff) inside the else block.

Another thing, as @Brian Roach so helpfully explains, computers are very case sensitive (e.g. "THUS" does not equal "thus"). So, make sure you're referring to the correct item.

fireshadow52
  • 6,298
  • 2
  • 30
  • 46
  • Pretty sure auto-boxing already takes care of this, but I'd have to test it. If it's not puking it's certainly converting the `long` to *some* `Object` – Brian Roach Jan 12 '12 at 15:52
  • @BrianRoach Not sure, but there isn't any detriment to making the conversion. – fireshadow52 Jan 12 '12 at 15:58
  • @Brian Roach and @ fireshadow52 well thanks alot for your help. i've dne the changes and it works:) but there is a small problem. when i click the start button i get a message box..How to correct it. – Ravi77 Jan 12 '12 at 15:59
  • @fireshadow52 - sure there is; an unneeded concatenation and 2 `String` objects ;) I just tested it - auto boxing does this for you and it's not needed. – Brian Roach Jan 12 '12 at 16:02