I have a project written using Swing, and I want to make it more smoothly (like JavaFX is) by adding animation to some components(JButton, JScrollPane, JSplitPane) using javax.swing.Timer.

UPD: That is not a game. I want to use the Timer for short animations like mouseHover events or dropdown, or scroll. But the problem is, a lot of Timer objects should be created.

Question: What action does it perform for JVM? I would start and stop a lot of Timers during app session.

  • 1
    Sharing your research helps everyone. Tell us what you've tried and why it didn’t meet your needs. This demonstrates that you’ve taken the time to try to help yourself, it saves us from reiterating obvious answers, and most of all it helps you get a more specific and relevant answer. Also see How to Ask – gnat Oct 7 '13 at 15:35

Yes if you wanna do the animation yourself, javax.swing.Timer is your best option. It is lightweight and guarantees your code executes on the EDT thread. If you venture into the depths of swing components like JScrollPane you might encounter javax.swing.Timer being used for things like auto scrolling during drag and drop etc. In short animation in swing is quite possible but rare in standard components. Just keep your redraw area to the minimum and repaint wisely. I would advocate looking into timing framework though.

| improve this answer | |

https://stackoverflow.com/questions/15040989/issues-creating-a-very-accurate-swing-timer Shows swing timer being off dead accurate time by 10ms to 200ms using it to trigger frames of animation won't work well. Using it to trigger the start of an animation should be OK. The way swing timers work is one thread manages the timeouts and the actual event callback happens on the EDT so it shouldn't matter if you have a large number of timers they don't use that many resources, just make sure the EDT can keep up.

| improve this answer | |
  1. No. Run your app with something akin to a game loop and update your components periodically that way. With a timer, you're going to get unreliable and choppy animation. This topic is too big to cover in detail here, but that's the basic idea.
  2. Implementing animation with Swing is fine, but just don't use a timer.


| improve this answer | |
  • No, the animation should be applied to short moments like mouseHover, dropdown etc... – SeniorJD Oct 7 '13 at 20:49
  • I think that a Swing Time is good for animating GUI stuff. You wrote that "This topic is too big to cover..." I don't know what you mean. It seems to me that if your animation is "choppy" that you would pause and wait for the vertical retrace to happen. – Kaydell Oct 8 '13 at 14:21
  • @Kaydell Choppy as in smooth animation happens at 30-60 frames a second. The timer noticing it has expired and needs to fire its event won't happen reliably at time intervals small enough to make 60 frames a second possible. – stonemetal Oct 10 '13 at 14:41

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.