Can some one please guide me on creating a JSpinner that only supplies value on MouseRelease Events. As dont want to send the intended application updates as spinners contents while rapidly changing. (there needs to be a timeSinceRelease accounted for.) but Its what i need to write like the particular listeners that work in with what I have so far -

import javax.swing.*;
import javax.swing.JSpinner;
import java.awt.Container;
import java.awt.event.MouseEvent;
import java.awt.event.MouseAdapter;
import javax.swing.SpinnerModel;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;

public class NewSpinner extends JPanel implements ChangeListener{
	
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	private  JSpinner spinner;

    public NewSpinner(final boolean cycleObjects) {
        super(new SpringLayout());

        final String[]label = {"Variable1: "};//, "Variable2: "};
        final int numPairs = label.length;
        JFormattedTextField ftf = null;

        //populate the dataObject
        final int dataLength = 10000;
        final Integer[] dataObjects = new Integer[dataLength];
        for(int i =0;i<dataLength;i++){
        	dataObjects[i] = i;
        }
      
    	//Add the label-spinner pair.                     
        SpinnerListModel objectModel = null;
        if (cycleObjects) { //use custom model
            objectModel = new CyclingSpinnerListModel(dataObjects);
        } else { //use standard model
            objectModel = new SpinnerListModel(dataObjects);
        }

        final JSpinner spinner = addLabeledSpinner(this, label[0], objectModel);
        
        //Tweak the spinner's formatted text field.
        ftf = getTextField(spinner);
        if (ftf != null ) {
            ftf.setColumns(8); //specify more width than we need
            ftf.setHorizontalAlignment(JTextField.RIGHT);
        }
        //listen for changes on the numSpinner
        spinner.addChangeListener(this);
        
        //Lay out the panel.
        SpringUtilities.makeCompactGrid(this,
        								numPairs, 2, //rows, cols
                                        10, 50,        //initX, initY
                                        6, 10);       //xPad, yPad
        
        System.out.println("mouse listener being added");     
        spinner.addMouseListener(new MouseAdapter(){
        	public void mouseClicked(MouseEvent me){
        		if (spinner.equals(me.getSource())){
        			System.out.println(" mouse event");
        		}
        	}
        });


	}	
    /**
     * Return the formatted text field used by the editor, or
     * null if the editor doesn't descend from JSpinner.DefaultEditor.
     */   
       
    public JFormattedTextField getTextField(final JSpinner spinner) {
        final JComponent editor = spinner.getEditor();
        if (editor instanceof JSpinner.DefaultEditor) {
            return ((JSpinner.DefaultEditor)editor).getTextField();
        } else {
            System.err.println("Unexpected editor type: "
                               + spinner.getEditor().getClass()
                               + " isn't a descendant of DefaultEditor");
            return null;
        }
    }	
    // Required by the ChangeListener interface.
    // Listens for changes in the spinner and responds
    
	public void stateChanged(final ChangeEvent event) {
    	System.out.println("There is an event");
 
		}
	public void mousePressed(MouseEvent me){
		System.out.println("mouse pressed event");
	}
	public Object getValue(){
		Object thisObject = new Object;
		// if (timeSinceRelease < 2 seconds) { then valueObject = lastValue}
		// if (timeSinceRelease >= 2 seconds){ then valueObject = JSpinner.getModel().getValue(); lastValue=valueObject}
		
		return thisObject;		
	}			
    static protected JSpinner addLabeledSpinner(final Container c, final String label, final SpinnerModel model)
    {
        final JLabel l = new JLabel(label);
        c.add(l);

        final JSpinner spinner = new JSpinner(model);
        l.setLabelFor(spinner);
        c.add(spinner);
        
        return spinner;
    }

    /**
     * Create the GUI and show it.  For thread safety,
     * this method should be invoked from the
     * event-dispatching thread.
     */
    private static void createAndShowGUI() {
        //Create and set up the window.
        final JFrame frame = new JFrame("NewSpinner");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        //Create and set up the content pane.
        final JComponent newContentPane = new NewSpinner(false);
        newContentPane.setOpaque(true); //content panes must be opaque
        frame.setContentPane(newContentPane);

        //Display the window.
        frame.pack();
        frame.setVisible(true);
    }

    public static void main(final String[] args) {
        //Schedule a job for the event-dispatching thread:
        //creating and showing this application's GUI.
        javax.swing.SwingUtilities.invokeLater(new Runnable() {
            public void run() {
                createAndShowGUI();
            }
        });
    }
}

This runs with SpringUtilities from http://java.sun.com/docs/books/tutorial/uiswing/components/spinner.html

As you've probably noticed, JSpinner does it's own thing with the mousePressed and mouseReleased events internally, so your added MouseListener doesn't respond to these. This leaves you with working through the ChangeListener alone. Adding a timer to poll for changes and checking for a minimum time between changes, you can limit the response like you are wanting to. Here's a ChangeListener you can add to the spinner to handle this

private class SpinnerListener implements ChangeListener, ActionListener {
    // minimum time in ms between update requests
    static final int MIN_TIME = 100;
    long lastChange = System.currentTimeMillis();

    boolean valueChanged=false;
    Timer repeatTimer;

    public SpinnerListener(){
        // timer polling every 50ms
        repeatTimer = new Timer(50, this);
        repeatTimer.start();
    }

    public void stateChanged(ChangeEvent e) {
        valueChanged=true;
        lastChange = System.currentTimeMillis();
    }

    // action called by timer
    public void actionPerformed(ActionEvent e) {
        if (valueChanged && (System.currentTimeMillis()-lastChange)>MIN_TIME){
            // call your update routine here
            System.out.println("updating stuff");
            valueChanged=false;
        }
    }
}

I arbitrarily chose the timer interval and the MIN_TIME constraint to balance reponsiveness of the update call. You can play with those a bit if you need to tweak the delay.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.