Java - Unable to change JText field value from DocumentListener methods

I get the exception "Attempted mutation in the notification." 1. How can I change it? 2. How can I get the value that was inside the TextField before the listener was called?

EDIT:

It is something like this. On the JText field, I have this listener

basePriceTF.getDocument().addDocumentListener(new DocumentListener(){ 
     public void insertUpdate(DocumentEvent e){ 
        if (Integer.getValue(basePriceTF.getText())<0){
        basePriceTF.setText("0");
        }
     }

     public void removeUpdate(DocumentEvent e){/**my implemntation**/}

     public void changedUpdate(DocumentEvent e){/**my implemntation**/}
}

insertUpdate() will probably cause a loop.
So it doesnt let me change inside basePriceTF.
+3
source share
3 answers

As pointed out three times so far, this is exactly what DocumentFilter. Here is the SSCCE that does what you want:

import javax.swing.JFrame;
import javax.swing.SwingUtilities;
import javax.swing.JTextField;
import javax.swing.text.AbstractDocument;
import javax.swing.text.Document;
import javax.swing.text.DocumentFilter;
import javax.swing.text.BadLocationException;
import javax.swing.text.AttributeSet;

public class TestDocumentFilter extends JFrame {
  JTextField basePriceTF;

public TestDocumentFilter() {
    super("TestDocumentFilter");
    basePriceTF = new JTextField();
    AbstractDocument basePriceDocument = (AbstractDocument) basePriceTF.getDocument();
    basePriceDocument.setDocumentFilter(new PositiveIntegerFilter());
    getContentPane().add(basePriceTF);
}

/**
 * Resets the document to "0" for input values that do not constitut a non-negative integer.
 */
private static class PositiveIntegerFilter extends DocumentFilter {

    @Override
    public void replace(DocumentFilter.FilterBypass fb, int offset, int length, String inputTextValue,
            AttributeSet attrs) throws BadLocationException {
        Document oldDoc = fb.getDocument();
        String textValue = oldDoc.getText(0, oldDoc.getLength()) + inputTextValue;
        Integer basePrice = 0;
        try {
            basePrice = Integer.parseInt(textValue);
        } catch (NumberFormatException e) { 
            basePrice = 0;
        }
        if (basePrice < 0) 
            basePrice = 0;
        fb.replace(0, oldDoc.getLength(), basePrice.toString(), attrs);
    }
}

public static void main(String[] args) { 
    SwingUtilities.invokeLater(new Runnable() {
        @Override
        public void run() {
            JFrame frame = new TestDocumentFilter();
            frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            frame.pack();
            frame.setVisible(true);
        }
    });
}
}

You cannot enter "-1"; field reset to "0" when typing "-". Pay attention to FilterBypasswhich avoids any recursive call.

+4

DocumentListener API DocumentListeners , : .

- , , DocumentFilter .

+3

This is probably not a good practice, but it should work, and it does not require any complicated and expensive things.

final Document doc = basePriceTF.getDocument();
basePriceTF.getDocument().addDocumentListener(new DocumentListener(){
     public void insertUpdate(DocumentEvent e){
        if (Integer.getValue(basePriceTF.getText())<0){
            new Thread(new Runnable() {
                public void run() {
                    doc.removeDocumentListener(this);
                    basePriceTF.setText("0");        
                    doc.addDocumentListener(this);
                }
            }).start();
        }     
     }
     .......

You can also use SwingUtilities.invokeLater () if you are afraid to execute the code at the wrong time. It should also work.

+1
source

All Articles