0

I have created a software timer and when it goes zero it's going to start a new login screen. The problem is the login comes over and over again. How to stop this?

class DisplayCountdown extends TimerTask {
 int seconds = 0005;
 public void run() {
 if (seconds > 0) {
 int hr = (int) (seconds / 3600);
 int rem = (int) (seconds % 3600);
 int mn = rem / 60;
 int sec = rem % 60;
 String hrStr = (hr < 10 ? "0" : "") + hr;
 String mnStr = (mn < 10 ? "0" : "") + mn;
 String secStr = (sec < 10 ? "0" : "") + sec;
 seconds--;
 lab.setText(hrStr + " : " + mnStr + " : " + secStr + "");
 } else {
 login ty = new login();
 login.scname.setText(scname.getText());
 login.scnum.setText(scnum.getText());
 login.mar.setText(jTextField1.getText());
 ty.setVisible(true);
 dispose();
 }
 }
}
MadProgrammer
348k22 gold badges243 silver badges379 bronze badges
asked Oct 7, 2013 at 5:46

3 Answers 3

1

This is violating the single thread rules of Swing - Updating the UI out side the context of the Event Dispatching Thread.

Instead of TimerTask, you should be using a javax.swing.Timer.

javax.swing.Timer swingTimer = new javax.swing.Timer(500, new ActionListener() {
 public void actionPerformed(ActionEvent evt) {
 if (seconds > 0) {
 //...
 } else {
 ((javax.swing.Timer)evt.getSource()).stop();
 //...
 }
 }
});

Take a look at Concurrency in Swing

answered Oct 7, 2013 at 5:53
Sign up to request clarification or add additional context in comments.

Comments

0

It is not clear what the code is but can't you use the "cancel()" method of the TimerTask class to cancel it?

answered Oct 7, 2013 at 5:50

Comments

0

You need to keep reference of previous Login Screen and dispose it before making a new one;

answered Oct 7, 2013 at 6:06

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.