Hi,
I encountered the same problem, this is how I solved the implementation for my actions scenario:
First I collect all arrow buttons:
private static HashMap<String, BasicArrowButton> getSpinnerButtons(JSpinner spinner, String[] arrowNames) {
final Stack<String> arrows = new Stack<String>();
arrows.addAll( Arrays.asList( arrowNames ) );
final HashMap<String, BasicArrowButton> buttons = new HashMap<String, BasicArrowButton>();
while (buttons.size()<2) {
for (final Component c : spinner.getComponents()) {
if (c instanceof BasicArrowButton) {
final BasicArrowButton bab = (BasicArrowButton)c;
for (final String sName : arrows) {
if (sName.equals(bab.getName())&&!buttons.containsKey(sName)) {
buttons.put(sName,bab);
break;
}
}
}
}
}
return buttons;
}
Then I attach some listener:
final String KEY_PROP = ".DIRECTION";
final String BS = spinner.getName(), BN="Spinner.nextButton", BP="Spinner.previousButton";
final String BSKEY = BS+KEY_PROP, BNKEY = BN+KEY_PROP, BPKEY = BP+KEY_PROP;
final HashMap<String, BasicArrowButton> buttons = getSpinnerButtons(spinner, new String[]{BN,BP});
spinner.putClientProperty( BSKEY, 1000);
spinner.putClientProperty( BNKEY, buttons.get(BN).getDirection()*+10000);
spinner.putClientProperty( BPKEY, buttons.get(BP).getDirection()*-10000);
final PropertyChangeListener pcl = new PropertyChangeListener() {
@Override
public void propertyChange(PropertyChangeEvent evt) {
final JSpinner spinnerRef = ((JSpinner)evt.getSource());
final String pName = evt.getPropertyName();
final short swing = Short.parseShort( String.valueOf(evt.getOldValue()) );
final short val = Short.parseShort( String.valueOf(evt.getNewValue()) );
if (Math.abs(swing)<2D)
System.out.printf("This is a DIRECTION CHANGE\nOld Direction=%s;\nNew Direction=%s;\nProp Value: %s", swing, val, spinnerRef.getClientProperty(pName) ).println();
else //arrows
System.out.printf("This is the CURRENT DIRECTION\nArrow=%s;\nDirection=%s;\nProp Value: %s", swing, val, spinnerRef.getClientProperty(pName) ).println();
System.out.println("==============");
}
};
spinner.addPropertyChangeListener(BSKEY, pcl);
spinner.addPropertyChangeListener(BNKEY, pcl);
spinner.addPropertyChangeListener(BPKEY, pcl);
final ActionListener spinnerActions = new ActionListener() {
private short oldDir=0;
@Override
public void actionPerformed(ActionEvent e) {
final BasicArrowButton bab = ((BasicArrowButton)e.getSource());
final short swingDir = (short)bab.getDirection();
final short newDir = (swingDir!=SwingConstants.NORTH&&swingDir!=SwingConstants.WEST) ? Integer.valueOf(-1).shortValue() : Integer.valueOf(+1).shortValue();
bab.getParent().firePropertyChange(bab.getName()+KEY_PROP, swingDir*1000, newDir);
bab.getParent().firePropertyChange(bab.getParent().getName()+KEY_PROP, oldDir, newDir);
this.oldDir=newDir;
}
};
buttons.get(BP).addActionListener(spinnerActions);
buttons.get(BN).addActionListener(spinnerActions);