How to check manual edits on a JSpinner field using DefaultEditor approach

后端 未结 4 2198
孤城傲影
孤城傲影 2021-01-24 06:07

I am adapting code from here:

Value Change Listener to JTextField

EDIT 2

The following code gives me an infinite loop of dialogs when I press the up spin

4条回答
  •  独厮守ぢ
    2021-01-24 06:22

    Custom DocumentListeners and formattedTextField don't play nicely with each other, better don't mix. Instead, use a PropertyChangeListener on the text field that listens for changes of its editValid property: whenever that changes to false, you could notify the users

    field.addPropertyChangeListener(new PropertyChangeListener() {
    
        @Override
        public void propertyChange(PropertyChangeEvent evt) {
            LOG.info("" + evt);
            if ("editValid".equals(evt.getPropertyName()) 
                &&  Boolean.FALSE.equals(evt.getNewValue())) {
              SpinnerNumberModel model = (SpinnerNumberModel) spin2.getModel();  
              JOptionPane.showMessageDialog(null,
              "Error: Number must be in range [" + model.getMinimum() + " ..." + model.getMaximum() + "]",
               "Error Massage",
              JOptionPane.ERROR_MESSAGE);
    
            }
    
        }
    });
    

    BTW, personally, I agree with Mad - such an intrusive notification tends to annoy me and maybe your users as well ..

提交回复
热议问题