Hello guys,
Im tring to make a stopwatch display in JTextField but so far i'am unable too :-(.
The stopWatch itself is working perfectly but only in the system output window...
I want the time to be displayed in the textfield(or Label) which is within a JFrame.
I'll appreciate any help and suggestion :-)
Here is the code for the stopWatch.java
package watch;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.Timer;
public class Watch
{
String time = "0";
int hours = 0;
int minutes = 0;
int seconds = 0;
boolean runTimer = true;
public Watch()
{
Timer timer = new Timer(1000, new TimerListenerClass());
timer.start();
while (runTimer)
{
time = hours + minutes + seconds + "";
System.out.printf("%2d : %2d : %2d%n", hours, minutes, seconds);
}
}
class TimerListenerClass implements ActionListener
{
@Override
public void actionPerformed(ActionEvent arg0)
{
seconds += 1;
if (seconds > 59)
{
seconds = 0;
minutes += 1;
}
if (minutes > 59)
{
minutes = 0;
hours += 1;
}
}
}
public static void main(String[]args)
{
new Watch();
}
}
here is the code for the JFrame
package watch;
import java.awt.BorderLayout;
import javax.swing.JFrame;
import javax.swing.JTextField;
/**
*
* @author Taimoor
*/
public class Frame extends JFrame
{
Watch watch;
JTextField tF;
public Frame()
{
setTitle("text");
setSize(600,400);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setLayout(new BorderLayout());
watch = new Watch();
tF = new JTextField("...",5);
tF.setText(watch.time);
add(tF, BorderLayout.CENTER);
setVisible(true);
}
public static void main(String[] args)
{
Frame frame = new Frame();
}
}