tags:

views:

119

answers:

2

I have a few JSpinners and they are numeric values starting at 10 and can be incremented until 99.

In my program the user has 15 points to disperse evenly across 6 skills. Every JSpinner has an EventListener to detect if its pressed, but more specifically I need to know which button was pressed so I know which action to take. They dont want to take a point off of strength and have it decrement the Total Points by 1, Instead if the Decrement is pressed it should Add 1.

What would be the best method to execute this?

(Also I am using NetBeans so a bit of the program is autoGenerated.)

A: 

Presumably you are somewhere inside a ChangeListener's stateChanged method - take a look at ChangeEvent#getSource()


Ok, your edit made my original answer pretty pointless.

Would creating your own SpinnerModel be a viable option?

Lauri Lehtinen
I already have a SpinnerModel for each JSpinner that defines the default value, incrementation, and such.
Kitsune
Did you look at overriding the `getNextValue` and `getPreviousValue` methods?
Lauri Lehtinen
No,that is not getting me to where I need to be.
Kitsune
Alright - I'm out of options then, I didn't find any other hooks to figuring out which button (up or down) is pressed.
Lauri Lehtinen
Thanks anyway though :\ this is a very complex problem and I really can't figure it out.
Kitsune
A: 

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);
Steel Plume