2

I am working on a Tetris project and I am finding that I need to use both javax.swing.Timer and java.util.Timer. But one of them is not working. Can anyone provide any insight into this problem?

2
  • yes you can, what was it that did not work? Commented Jun 11, 2013 at 18:30
  • If you want to use them in the same file, you are probably doing something wrong. Commented Jun 11, 2013 at 19:39

4 Answers 4

2

yes, you can use 2 classes with the same name, but rather than importing them, you should use the full qualifier:

ie:

javax.swing.Timer timer = new ...
Sign up to request clarification or add additional context in comments.

Comments

1

Yes you can use the 2 classes together but you have to enter the full package like this

java.util.Timer myUtilTimer= ..
javax.swing.Timer  myUtilTimer= ..

Besides you have to take care that output from java.util.Timer must be wrapped into SwingUtilities.invokeLater(myRunnable); to add to the EDT otherwise is no guaranted that anything should be diplayed/changed/repainted

2 Comments

I agree with @AndrewThompson, but because you fixed it, +1 from me.
You're welcome. Since I'm here, I deleted some noise (the old comments).
1

Yes, you can but if you are using both in the same java file, you will need to access one of the class by the full package name like java.util.Timer because it will get a conflict if you import both of the classes.

Comments

1

In this concrete example, "A javax.swing.Timer increments an int at 100 Hz, while a java.util.TimerTask samples the value at 1 Hz." A LinkedBlockingQueue<Integer> synchronizes access to the shared data and records the last N samples. The example illustrates features of all three preceding answers:

  • Use of a fully qualified name.

  • Use of invokeLater().

Comments

Your Answer

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