Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / GUI / July 2004

Tip: Looking for answers? Try searching our database.

Set the speed of a JSpinner?

Thread view: 
Ian McCall - 13 Jul 2004 13:46 GMT
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.

Cheers,
Ian
VisionSet - 13 Jul 2004 22:12 GMT
> 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


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.