Which JSpinner button was clicked?

Is it possible to find out, from within the ChangeListener receiving the ChangeEvent from JSpinner, which button (increment / decrement) was pressed?

+5
source share
5 answers

Short answer: There is no way to know which button was pressed.

Long answer: depending on your model and your change listener, if you are comparing between a new value and a previous value, you can find out if the user went forward or backward.

+5
source

You can check the object that triggers the event. Perhaps save the value before the event and determine if it was up or down during the event.

+1
source

. :

ChangeEvent ce = ...
((JSpinner)ce.getSource()).getPreviousValue();
0

, :

int currentValue = spinner.getValue();
spinner.addChangeListener(new javax.swing.event.ChangeListener() {
    public void stateChanged(javax.swing.event.ChangeEvent e) {
        int value = spinner.getValue();
        if(value > currentValue) {
            // up was pressed
        } else if(value < currentValue) {
            // down was pressed
        }
        currentValue = value;
    }
});
0

JSpinner is an integral component, it can be added to the components that it contains. You will have to experiment a bit to determine how to distinguish buttons from each other and from the text field. One quick and dirty way would be to check their coordinates.

I'm not sure if you want to iterate the components contained in JSpinner itself or those contained in the container returned JSpinner.getEditor(), so try both.

0
source

All Articles