I get an "Attempt to mutate in notification" exception. 1. How can I change it ? 2. How can I get the value that was inside the TextField before the listener was triggered?
EDIT:
It is something like this. On the JTextfield I have this listener
basePriceTF.getDocument().addDocumentListener(new DocumentListener(){
public void insertUpdate(DocumentEvent e){
if (Integer.getValue(basePriceTF.getText())<0){
basePriceTF.setText("0");
开发者_如何学C }
}
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.
As was pointed out three times so far,
this is exactly what DocumentFilter
s are made for.
Here is a 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"; the field is reset to "0" when "-" is typed. Note the FilterBypass
which avoids any recursive call.
Per the DocumentListener tutorial and API, DocumentListeners should not modify the contents of the Document, so the short answer is: don't do this.
Consider using something else perhaps a DocumentFilter or some other validating method.
This is probably not a good practice, but it should work, and it doesn't involve any complicated nor any expensive stuff.
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 moment. It should also work.
精彩评论