How do I know if increment/decrement on JSpinner is pressed?

99封情书 提交于 2019-12-13 03:49:36

问题


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.)


回答1:


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?




回答2:


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


来源:https://stackoverflow.com/questions/3090502/how-do-i-know-if-increment-decrement-on-jspinner-is-pressed

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!