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