java - 无法从 DocumentListener 方法内部更改 JTextfield 的值 [英] java - Cant change the value of a JTextfield from inside of DocumentListener methods

查看:28
本文介绍了java - 无法从 DocumentListener 方法内部更改 JTextfield 的值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我收到尝试在通知中进行变异"异常.1. 我怎样才能改变它?2.如何获取触发监听器之前TextField中的值?

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?

事情是这样的.在 JTextfield 我有这个监听器

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

     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.

推荐答案

到目前为止已经被指出三遍,这正是 DocumentFilter 的用途.这是一个 SSCCE 可以满足您的需求:

As was pointed out three times so far, this is exactly what DocumentFilters 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);
        }
    });
}
}

不能输入-1";键入-"时,该字段将重置为0".注意 FilterBypass 避免任何递归调用.

You cannot enter "-1"; the field is reset to "0" when "-" is typed. Note the FilterBypass which avoids any recursive call.

这篇关于java - 无法从 DocumentListener 方法内部更改 JTextfield 的值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆