PropertyChangeSupport for SpinnerNumberModel

2.8k Views Asked by At

I want to listen to the changes of the value of the SpinnerNumberModel for a JSpinner.
I create a PropertyChangeSupport and put the model into it.

I need the propertyChangeListener, because it shows me the old and new value of the property.

The snippet doesn't work: the propertyChange method prints nothing, when I click on the JSpinner.
A simple ChangeListener give only the new value, but I need also the old value, how can I get it?

package de.unikassel.jung;

import java.beans.PropertyChangeEvent;
import java.beans.PropertyChangeListener;
import java.beans.PropertyChangeSupport;

import javax.swing.JFrame;
import javax.swing.JSpinner;
import javax.swing.SpinnerNumberModel;

public class PropertyChangeTest implements PropertyChangeListener {

    public static void main(String[] args) {
        new PropertyChangeTest();
    }

    public PropertyChangeTest() {
        JFrame frame = new JFrame();
        frame.setBounds(100, 100, 450, 300);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        int value = 1;
        int min = 0;
        int max = 10;
        int step = 1;
        SpinnerNumberModel spinnerModel = new SpinnerNumberModel(value, min, max, step);

        PropertyChangeSupport pcs = new PropertyChangeSupport(spinnerModel);
        pcs.addPropertyChangeListener("value", this);

        JSpinner spinner = new JSpinner(spinnerModel);
        frame.getContentPane().add(spinner);
        frame.setVisible(true);
    }

    @Override
    public void propertyChange(PropertyChangeEvent evt) {
        System.out.println(evt);
        System.out.println(evt.getSource());
    }

}
4

There are 4 best solutions below

6
On BEST ANSWER

Instead of listening to the model, listen to the editor's JFormattedTextField, as suggested below.

JSpinner spinner = new JSpinner(new SpinnerNumberModel(1, 0, 10, 1));
JSpinner.DefaultEditor editor = (JSpinner.DefaultEditor) spinner.getEditor();
editor.getTextField().addPropertyChangeListener("value", this);
1
On

Monday morning ... classical time for not resisting a couple of comments :-)

@timaschew

  • "need the propertyChangeListener, because it shows me the old and new value of the property." - (nitpicking - but always have this strong urge to separate requirement and solution :), I think it's the other way round: on a change notification you need access to both the old and new value, a propertyChangeEvent/Listener is a notification type which supports it, there might be others
  • PropertyChangeSupport is not supposed to be used on part of the the observing code, it's supposed to be used on the obervable's side (just as @Hovercraft did in his example): it's sole responsibility is manage and notify the listeners registered to the observable
  • occasionally, accessibleContext provides a hook for hacks - nevertheless, it's a hack to hook into it (except you really need to support accessibility, which might well be the case :-) As with all hacks, that's a brittle solution which most probably will cause pain sometime in the future. Much more stable to follow the link about how Action and AbstractButton interact

@Hovercraft

  • enhancing the model with a richer change notification is the way-to-go (as in: my absolute favourite :-)
  • just a small detail: if you have a slave let him do all the work - PropertyChangeSupport has methods which take the old/new value, no need to feed to create an event on the observable. It will be thrown away anyway when old and new are equal
  • for newValue in the notification event, don't use the parameter but instead use getValue again (super might have rejected the change)

@trashgod

haha - you already guessed that I don't like solution: it breaks encapsulation in that it relies on an implementation detail, so don't except when in complete control of the JSpinner creation and are absolutely sure its editor is never changed

0
On

http://docs.oracle.com/javase/tutorial/javabeans/writing/properties.html#bound You have to fireThePropertyChange in the setters.

3
On

For a PropertyChangeSupport to work you need to call its firePropertyChange method, but more importantly the support object needs to have access to the setXXX method of the property that it is listening to, and in that method it needs to call PropertyChangeSupport's firePropertyChange method. And so I think for your idea to work, you'll need to extend the model's class, give it a PropertyChangeSupport object, give it the add and remove listener methods, and be sure to listen to changes made in the model's setValue method which is key. In my example that method looks like this:

   @Override
   public void setValue(Object newValue) {
      // store old value and set the new one
      Object oldValue = getValue();
      super.setValue(newValue);

      // construct the event object using these saved values
      PropertyChangeEvent evt = new PropertyChangeEvent(this, VALUE, oldValue,
               newValue);

      // notify all of the listeners
      pcs.firePropertyChange(evt);
   }

Here's my sample model class that uses PropertyChangeSupport:

import java.beans.*;
import javax.swing.*;
import javax.swing.event.*;

@SuppressWarnings("serial")
class MySpinnerNumberModel extends SpinnerNumberModel {
   public static final String VALUE = "value";
   private SwingPropertyChangeSupport pcs = new SwingPropertyChangeSupport(this);

   // you will likely need to create multiple constructors to match
   // the ones available to the SpinnerNumberModel class
   public MySpinnerNumberModel(int value, int min, int max, int step) {
      super(value, min, max, step);
   }

   public void addPropertyChangeListener(PropertyChangeListener listener) {
      pcs.addPropertyChangeListener(listener);
   }

   public void removePropertyChangeListener(PropertyChangeListener listener) {
      pcs.removePropertyChangeListener(listener);
   }

   @Override
   public void setValue(Object newValue) {
      // store old value and set the new one
      Object oldValue = getValue();
      super.setValue(newValue);

      // construct the event object using these saved values
      PropertyChangeEvent evt = new PropertyChangeEvent(this, VALUE, oldValue,
               newValue);

      // notify all of the listeners
      pcs.firePropertyChange(evt);
   }
}

And finally the test class to test out the above class to see if it is working properly:

import java.beans.*;
import javax.swing.*;

public class TestSpinnerPropChange {

   private static void createAndShowUI() {
      final MySpinnerNumberModel myModel = new MySpinnerNumberModel(1, 0, 10, 1);
      final JSpinner spinner = new JSpinner(myModel);

      final JTextField oldValueField = new JTextField(10);
      final JTextField newValueField = new JTextField(10);

      JPanel panel = new JPanel();
      panel.add(spinner);
      panel.add(new JLabel("old value:"));
      panel.add(oldValueField);
      panel.add(new JLabel("new value:"));
      panel.add(newValueField);

      myModel.addPropertyChangeListener(new PropertyChangeListener() {
         public void propertyChange(PropertyChangeEvent evt) {
            // checking the property name is overkill here, but is a good habit
            // to get into, especially if listening to more than one property.
            if (evt.getPropertyName().equals(MySpinnerNumberModel.VALUE)) {
               oldValueField.setText(evt.getOldValue().toString());
               newValueField.setText(evt.getNewValue().toString());
            }
         }
      });

      JFrame frame = new JFrame("TestSpinnerPropChange");
      frame.getContentPane().add(panel);
      frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
      frame.pack();
      frame.setLocationRelativeTo(null);
      frame.setVisible(true);
   }

   public static void main(String[] args) {
      java.awt.EventQueue.invokeLater(new Runnable() {
         public void run() {
            createAndShowUI();
         }
      });
   }
}