Question

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 spinner arrow:

STRING: STRING: 10 VALS: 10 STRING: STRING: 10 VALS: 10 STRING: STRING: 10 VALS: 10 .....

Warning you will need to use taskmanager to kill it.

    public static void main(String[] args) {
    // TODO Auto-generated method stub

    JFrame F = new JFrame();
    F.setVisible(true);
    JPanel p = new JPanel();


    final JSpinner spin2 = new JSpinner();
    spin2.setModel(new SpinnerNumberModel(10, 10, 100, 1));

    JComponent comp = spin2.getEditor();
    JFormattedTextField field = (JFormattedTextField) comp.getComponent(0);
    DefaultFormatter formatter = (DefaultFormatter) field.getFormatter();
    formatter.setCommitsOnValidEdit(true);


    ((JSpinner.DefaultEditor)spin2.getEditor()).getTextField().getDocument().addDocumentListener(new DocumentListener() {
          public void changedUpdate(DocumentEvent e) {
                warn();
              }
              public void removeUpdate(DocumentEvent e) {
                warn();
              }
              public void insertUpdate(DocumentEvent e) {
                warn();
              }

              public void warn() {
                  String text = ((JSpinner.DefaultEditor)spin2.getEditor()).getTextField().getText();
                  JOptionPane.showMessageDialog(null,   "STRING: "+text, "Error Massage",     JOptionPane.ERROR_MESSAGE);
                  if (text != null && !text.trim().isEmpty()) {
                      int stringValue = Integer.parseInt(((JSpinner.DefaultEditor)spin2.getEditor()).getTextField().getText());
                      JOptionPane.showMessageDialog(null,
                              "VALS: "+spin2.getValue(), "Error Massage",
                              JOptionPane.ERROR_MESSAGE);
                     if (stringValue<10 || stringValue >100){
                       JOptionPane.showMessageDialog(null,
                          "Error: Number outside bounds", "Error Massage",
                          JOptionPane.ERROR_MESSAGE);
                     }

                  }
              }
            });


    p.add(spin2);   


    F.add(p);
    F.pack();
    F.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);


}

EDIT 3

This changes background to red when invalid, BUT values reverts to previous (if invalid) when field looses focus. I want to be able to put up a JOptionPane at that point saying value is STILL invalid, instead of reverting to previous:

        ((JSpinner.DefaultEditor)Position.getEditor()).getTextField().addPropertyChangeListener(new PropertyChangeListener() {

            @Override
            public void propertyChange(PropertyChangeEvent evt) {
                //LOG.info("" + evt);
                if ("editValid".equals(evt.getPropertyName())) {
                    if (Boolean.FALSE.equals(evt.getNewValue())) {
                        SpinnerNumberModel model = (SpinnerNumberModel) Position.getModel();  

                        ((JSpinner.DefaultEditor)Position.getEditor()).getTextField().setBackground(Color.RED);
                        ((JSpinner.DefaultEditor)Position.getEditor()).getTextField().setToolTipText("Amount must be in range [ " + model.getMinimum() + " ... " + model.getMaximum() + " ] for this symbol");

                    }
                    else{
                        ((JSpinner.DefaultEditor)Position.getEditor()).getTextField().setBackground(Color.WHITE);
                    }
                }

            }
        });

//////////////////////////////////////////////////////////////////////////////////////////////////

ORIGINAL QUESTION

But if I use spinner to put in value lower than lower bound. I get a "(" in the text field and this error:

Exception in thread "AWT-EventQueue-0" java.lang.NumberFormatException: For input string: "" at java.lang.NumberFormatException.forInputString(NumberFormatException.java:65) at java.lang.Integer.parseInt(Integer.java:504) at java.lang.Integer.parseInt(Integer.java:527) at com.NResearch.ValueAtRisk.Sigma$7.warn(Sigma.java:626) at com.NResearch.ValueAtRisk.Sigma$7.removeUpdate(Sigma.java:619) at javax.swing.text.AbstractDocument.fireRemoveUpdate(AbstractDocument.java:260) at javax.swing.text.AbstractDocument.handleRemove(AbstractDocument.java:623) at javax.swing.text.AbstractDocument.remove(AbstractDocument.java:591) at javax.swing.text.AbstractDocument.replace(AbstractDocument.java:667) at javax.swing.text.JTextComponent.setText(JTextComponent.java:1718) at javax.swing.JFormattedTextField$AbstractFormatter.install(JFormattedTextField.java:949) at javax.swing.text.DefaultFormatter.install(DefaultFormatter.java:124) at javax.swing.text.InternationalFormatter.install(InternationalFormatter.java:285) at javax.swing.JFormattedTextField.setFormatter(JFormattedTextField.java:465) at javax.swing.JFormattedTextField.setValue(JFormattedTextField.java:789) at javax.swing.JFormattedTextField.processFocusEvent(JFormattedTextField.java:636) at java.awt.Component.processEvent(Component.java:6261) at java.awt.Container.processEvent(Container.java:2229) at java.awt.Component.dispatchEventImpl(Component.java:4861) at java.awt.Container.dispatchEventImpl(Container.java:2287) at java.awt.Component.dispatchEvent(Component.java:4687) at java.awt.KeyboardFocusManager.redispatchEvent(KeyboardFocusManager.java:1895) at java.awt.DefaultKeyboardFocusManager.typeAheadAssertions(DefaultKeyboardFocusManager.java:938) at java.awt.DefaultKeyboardFocusManager.dispatchEvent(DefaultKeyboardFocusManager.java:570) at java.awt.Component.dispatchEventImpl(Component.java:4731) at java.awt.Container.dispatchEventImpl(Container.java:2287) at java.awt.Component.dispatchEvent(Component.java:4687) at java.awt.EventQueue.dispatchEventImpl(EventQueue.java:735) at java.awt.EventQueue.access$200(EventQueue.java:103) at java.awt.EventQueue$3.run(EventQueue.java:694) at java.awt.EventQueue$3.run(EventQueue.java:692) at java.security.AccessController.doPrivileged(Native Method) at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:76) at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:87) at java.awt.EventQueue$4.run(EventQueue.java:708) at java.awt.EventQueue$4.run(EventQueue.java:706) at java.security.AccessController.doPrivileged(Native Method) at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:76) at java.awt.EventQueue.dispatchEvent(EventQueue.java:705) at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:242) at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:161) at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:150) at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:146) at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:138) at java.awt.EventDispatchThread.run(EventDispatchThread.java:91)

final JSpinner spin2 = new JSpinner();
spin2.setModel(new SpinnerNumberModel(10, 10, 100, 1));

JComponent comp = spin2.getEditor();
JFormattedTextField field = (JFormattedTextField) comp.getComponent(0);
DefaultFormatter formatter = (DefaultFormatter) field.getFormatter();
formatter.setCommitsOnValidEdit(true);


((JSpinner.DefaultEditor)spin2.getEditor()).getTextField().getDocument().addDocumentListener(new DocumentListener() {
          public void changedUpdate(DocumentEvent e) {
                warn();
              }
              public void removeUpdate(DocumentEvent e) {
                warn();
              }
              public void insertUpdate(DocumentEvent e) {
                warn();
              }

              public void warn() {
                  int stringValue = Integer.parseInt(((JSpinner.DefaultEditor)spin2.getEditor()).getTextField().getText());
                  JOptionPane.showMessageDialog(null,
                          "VALS: "+spin2.getValue(), "Error Massage",
                          JOptionPane.ERROR_MESSAGE);
                 if (stringValue<10 || stringValue >100){
                   JOptionPane.showMessageDialog(null,
                      "Error: Please enter number bigger than 0", "Error Massage",
                      JOptionPane.ERROR_MESSAGE);
                 }

              }
            });
Was it helpful?

Solution

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

OTHER TIPS

You have a few basic choices.

  1. You could trap the exception
  2. You could check for a "empty" String

Personally, I'd like to do both...

public void warn() {
    String text = ((JSpinner.DefaultEditor)spin2.getEditor()).getTextField().getText();
    if (text != null && !text.trim().isEmpty()) {
        try {
            int stringValue = Integer.parseInt(text);
            JOptionPane.showMessageDialog(null,
                  "VALS: "+spin2.getValue(), "Error Massage",
                  JOptionPane.ERROR_MESSAGE);
            if (stringValue<10 || stringValue >100){
            JOptionPane.showMessageDialog(null,
              "Error: Please enter number bigger than 0", "Error Massage",
              JOptionPane.ERROR_MESSAGE);
            }
        } catch (NumberFormatException exp) {
            exp.printStackTrace();
        }
    }
}

Now, as a user, this is likely just to annoy me. Highlight the field, beep, change the tooltip, sure, throw a dialog in my face...hmmm...

You could take a look at Validating Input, which will allow you to validate the input when the field loses focus, which, personally, might be a better choice.

If you don't particularly need to functionality of the JSpinner (running values up and down in a sequence), you could take a look at using a DocumentFilter (for examples), which will allow you to control what goes into the field. You should know that it's not possible (or close enough to it) to add a DocumentFilter to a JSpinner... :P

The Exception says that the String you pass to Integer.parseInt(..) is an empty string. So make sure to check ((JSpinner.DefaultEditor)spin2.getEditor()).getTextField().getText() for null and empty before passing it to Integer.parseInt(..)

You may have a look at apache commons-io and the StringUtils class, they have quite a few good methods to ease the pain on checking for empty/blank strings

This proved to be a huge pain the rear. I wanted to color my text field background immediately. Ended up making an editor that overrode the editor I wanted to use, then setting it as the spinners editor accordingly. Seems to be working for me so far.

Add a java class with the following (remove the quotes around the code block, I'm having a hard time with stack overflow's editor):

`public class CustomNumberEditor extends JSpinner.NumberEditor {
    public CustomNumberEditor( JSpinner spinner ) {
        super( spinner );
        ((DefaultFormatter ) ((JFormattedTextField) getComponent( 0 )).getFormatter()).setCommitsOnValidEdit( true );
    }
    @Override public void propertyChange(PropertyChangeEvent e) {
        super.propertyChange( e );
        if( e.getPropertyName().equals( "value" ) )
            doStuff( (int) e.getNewValue() );
    }
    private void doStuff( int value )
    {
        //do stuff
    }
}`

Then when adding your spinner, do this:

_quantitySpinner.setEditor(new CustomNumberEditor(_quantitySpinner));
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top