> Is there any means of setting how quickly a JSpinner will spin through its
> list? I'm interested in limiting the rate for a number-based JSpinner, so to
> one change every third of a second if you keep the mouse button down.
Wierd, but anyway...
Something to start off with.
Not sure of the implications of using a thread that isn't the event thread
from the event thread.
You should really produce your own SpinnerModel and do something similar in
there.
I'm sure someone will shout me down for this...
import javax.swing.*;
public class Spinner extends JFrame {
static class MySpinner extends JSpinner {
public void setValue(final Object obj) {
new Thread(new Runnable() {
public void run() {
try {
Thread.sleep(333);
}
catch(InterruptedException e){
e.printStackTrace();
}
MySpinner.super.setValue(obj);
}
}).start();
}
}
public Spinner() {
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JMenuBar bar = new JMenuBar();
setJMenuBar(bar);
JMenu menu = new JMenu("File");
bar.add(menu);
menu.add(new JMenuItem("Test"));
getContentPane().add(new MySpinner());
setVisible(true);
pack();
}
public static void main(String[] args) {
new Spinner();
}
}

Signature
Mike W
Ian McCall - 13 Jul 2004 23:11 GMT
> You should really produce your own SpinnerModel and do something similar in
> there.
Thanks - eventually did this, but was hoping to avoid it as it now means
I'll have to implement this in every model rather than just being able to
use a different closing component.
The code, for anyone interested in the future:
import javax.swing.SpinnerNumberModel;
public class TimedSpinnerNumberModel extends SpinnerNumberModel {
public final static long DEFAULT_SCROLL_INTERVAL = 1000;
Long lastUpdate;
public TimedSpinnerNumberModel(double value, double minimum, double
maximum, double stepSize) {
super(value, minimum, maximum, stepSize);
lastUpdate = null;
}
public Object getNextValue() {
Object value;
if((lastUpdate == null) || System.currentTimeMillis() >=
lastUpdate.longValue() + DEFAULT_SCROLL_INTERVAL) {
value = super.getNextValue();
lastUpdate = new Long(System.currentTimeMillis());
}
else {
value = getNumber();
}
return value;
}
}
~
~
~
~
"TimedSpinnerNumberModel.java" 30L, 721C