views:

58

answers:

5

I need a variable countdown timer for between 1-10 seconds, that can be stopped and restarted. What's a good set of UI elements to use for this? I need something intuitive that uses a fairly small amount of screen real estate, comparable to a normal-sized JButton. A windup kitchen egg timer would be the best physical analogy:

alt text

NOTE: I know how to implement timers and restart them, I just need to figure out what UI elements to use.

NOTE 2: I need a variable countdown timer. If the user wants 1 second, I want a 1 second timer. If the user wants an 8.2 second timer, I want an 8.2 second timer. The kitchen timer above is simple, the user just turns it to a certain amount and lets it go.

+3  A: 

Why not use a JProgressBar that starts off "full" and empties as the time decreases? You overlay the remaining time in seconds over the bar, therefore avoiding using additional screen real estate.

Adamski
That's an indicator. I need a way to both display and set.
Jason S
(although +1 since it does have aesthetic appeal as an indicator)
Jason S
A: 

I'd use a JSpinner for the setting; a javax.swing.Timer for the counting; and a single button, labelled "Start" or "Stop" as a function of the Timer state. Almost anything would do for display, but @Adamski's JProgressBar idea has appeal.

trashgod
+1  A: 

It is fairly simple... Labels and buttons should be fine.. Here are some examples

alt text

alt text

alt text

eugener
and how do I set it? I really would just like something as simple as an egg timer.
Jason S
Not sure what you mean by "set it"
eugener
A JLabel and 2 JButtons under it or on the sides should be enough. The rest is just styling
eugener
set it = decide to set the timer to 10 seconds or 1 second or 5 seconds.
Jason S
When the set button is pressed drop down menu appears with those times. So all together it would take 2 clicks. For more cutomization set of JSpinners can replace the time label just for setting time.
eugener
+1  A: 

For a short term solution I used a JSlider... I add an ActionListener to its BoundedRangeModel, and set my timer when there is a change and the BoundedRangeModel.getValueIsAdjusting() returns false. When my timer counts down but is not yet expired and the BoundedRangeModel.getValueIsAdjusting() returns false, I call BoundedRangeModel.setValue().

Not too happy with it but it kinda does what I want.

Jason S
For entertainment, you can dress up the `SliderUI`: http://stackoverflow.com/questions/2898259
trashgod
+1  A: 

A (ridiculously) simple solution: if you don't need too much graphical flair, just use a single JButton that displays the seconds remaining when the timer is running. When the timer's off, it displays "Start"; clicking it will begin the countdown. You could then stop (or pause) it by clicking when the timer is running.

derekerdmann