如何知道JSpinner上的递增/递减是否被按下?

时间:2010-06-22 05:28:58

标签: java jspinner

我有一些JSpinner,它们是从10开始的数值,可以递增到99.

在我的计划中,用户有15分可以均匀分散6种技能。每个JSpinner都有一个EventListener来检测它是否被按下,但更具体地说我需要知道按下了哪个按钮,所以我知道要采取哪个动作。他们不想从强度上取下一点,并将总点数减少1,而如果按下减量则应该加1。

执行此操作的最佳方法是什么?

(我也使用NetBeans,所以程序的一部分是autoGenerated。)

2 个答案:

答案 0 :(得分:1)

据推测,您位于ChangeListener stateChanged方法中的某个位置 - 请查看ChangeEvent#getSource()


好的,你的编辑让我的原始答案毫无意义。

创建自己的SpinnerModel是否可行?

答案 1 :(得分:-1)

我遇到了同样的问题,这就是我如何解决我的操作场景的实现:

首先我收集所有箭头按钮

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

然后我附上一些听众

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