首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >JFormattedTextField摧毁DocumentFilter

JFormattedTextField摧毁DocumentFilter
EN

Stack Overflow用户
提问于 2013-11-19 14:54:47
回答 1查看 1K关注 0票数 2

我对JFormattedTextField有一个问题(我将它用作所有文本字段的基类)。

今天,我尝试在这个字段的文档中添加一个文档过滤器,它工作得很好,但前提是它没有格式化程序工厂设置。

问题是,当设置格式化程序工厂(在我的示例中是默认类)并调用processFocusEvent时,会发生以下情况(JFormattedTextField.java:595):

代码语言:javascript
复制
    // if there is a composed text, process it first
    if ((ic != null) && composedTextExists) {
    ic.endComposition();
    EventQueue.invokeLater(focusLostHandler);
    } else {
    focusLostHandler.run();
    }
    }
    else if (!isEdited()) {
        // reformat
        setValue(getValue(), true, true);
    }

然后调用setValue() (JFormattedTextField.java:757):

代码语言:javascript
复制
private void setValue(Object value, boolean createFormat, boolean firePC) {
    Object oldValue = this.value;

    this.value = value;

    if (createFormat) {
        AbstractFormatterFactory factory = getFormatterFactory();
        AbstractFormatter atf;

        if (factory != null) {
            atf = factory.getFormatter(this);
        }
        else {
            atf = null;
        }
        setFormatter(atf);
    }
    else {
        // Assumed to be valid
        setEditValid(true);
    }

    setEdited(false);

if (firePC) {
    firePropertyChange("value", oldValue, value);
}
}

如您所见,如果有一个工厂,它将尝试“刷新”格式化程序。

(JFormattedTextField.java:439):

代码语言:javascript
复制
protected void setFormatter(AbstractFormatter format) {
    AbstractFormatter oldFormat = this.format;

    if (oldFormat != null) {
        oldFormat.uninstall();
    }
    setEditValid(true);
    this.format = format;
    if (format != null) {
        format.install(this);
    }
    setEdited(false);
    firePropertyChange("textFormatter", oldFormat, format);
}

这是真正的问题,我有(JFormattedTextField$AbstractFormatter.class:950):

代码语言:javascript
复制
    public void uninstall() {
        if (this.ftf != null) {
            installDocumentFilter(null);
            this.ftf.setNavigationFilter(null);
            this.ftf.setFormatterActions(null);
        }
    }

在这里,它破坏了我的文档过滤器,我知道格式化程序正常保存documentFilter,但是它真的打算那样工作吗?文档应该是处理其过滤器(imho)的对象,而不是格式化程序。有没有办法避免使用专门的格式化程序子类来绕过它?

示例代码:(按请求:)

代码语言:javascript
复制
package jftf;

import java.awt.Container;
import java.awt.Dimension;
import java.awt.FlowLayout;

import javax.swing.JFormattedTextField;
import javax.swing.JFrame;
import javax.swing.JTextField;
import javax.swing.text.AbstractDocument;
import javax.swing.text.AttributeSet;
import javax.swing.text.BadLocationException;
import javax.swing.text.DefaultFormatter;
import javax.swing.text.DocumentFilter;

/**
 * @author Pawel Miler
 */
public class JFormattedTextFieldExample {

private Container container;
private JFormattedTextField workingTextField;
private JFormattedTextField brokenTextField;
private DocumentFilter documentFilter;

public static void main(String[] args) {
    new JFormattedTextFieldExample();
}

public JFormattedTextFieldExample() {
    initializeDocumentFilter();
    initializeTextFields();
    initializeGui();
}

private void initializeDocumentFilter(){
    documentFilter = new UppercaseDocumentFilter();
}

private void initializeTextFields() {
    workingTextField = createTextField(false);
    addDocumentFilter(workingTextField);

    brokenTextField = createTextField(true);
    addDocumentFilter(workingTextField);
}

private JFormattedTextField createTextField(boolean createFormatter) {
    JFormattedTextField textField;
    textField = createFormatter ? new JFormattedTextField(new DefaultFormatter()) : new JFormattedTextField();
    return textField;
}

private void addDocumentFilter(JTextField textField) {
    ((AbstractDocument) textField.getDocument()).setDocumentFilter(documentFilter);
}

private void initializeGui() {
    container = createFrame();

    container.setLayout(new FlowLayout());

    Dimension dimension = new Dimension(80, 20);

    brokenTextField.setPreferredSize(dimension);
    container.add(brokenTextField);

    workingTextField.setPreferredSize(dimension);
    container.add(workingTextField);
}

private Container createFrame() {
    JFrame frame = new JFrame("JFormattedTextField example");
    frame.setSize(200, 70);
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.setVisible(true);

    return frame.getContentPane();
}

public class UppercaseDocumentFilter extends DocumentFilter {

    public void insertString(FilterBypass filterBypass, int offset, String text, AttributeSet attr) throws BadLocationException {
        super.insertString(filterBypass, offset, text.toUpperCase(), attr);
    }

    public void replace(DocumentFilter.FilterBypass filterBypass, int offset, int length, String text, AttributeSet attrs) throws BadLocationException {
        super.replace(filterBypass, offset, length, text.toUpperCase(), attrs);
    }
}
}

这两个文本字段应该具有相同的文档筛选器,但是输入一个,它总是会得到大写字母,而另一个则不会。

当前解决方案:(我刚才在JFormattedTextField的子类中实现的解决方案,如果格式化程序也有文档过滤器的话,我需要tu使用标志,您不能同时使用,但我一点也不高兴需要一个)

代码语言:javascript
复制
public boolean isPreserveDocumentFilter() {
    return preserveDocumentFilter;
}

public void setPreserveDocumentFilter(boolean preserveDocumentFilter) {
    this.preserveDocumentFilter = preserveDocumentFilter;
}

/**
 * We need to override if we want to use a documentFilter with DefaultFormatter implementation.
 * For more info see: <a href="http://stackoverflow.com/questions/20074778/jformattedtextfield-destroys-documentfilter">info</a>
 */
@Override
protected void setFormatter(AbstractFormatter format) {
    Document doc = this.getDocument();
    DocumentFilter filter = null;

    if (preserveDocumentFilter) {
        if ( doc instanceof AbstractDocument ) {
            filter = ((AbstractDocument) doc).getDocumentFilter();
        }
    }

    super.setFormatter(format);

    if ( filter != null ) {
        ((AbstractDocument) doc).setDocumentFilter(filter);
    }
}
EN

回答 1

Stack Overflow用户

发布于 2018-10-04 02:35:05

我也面临着同样的问题。基本上,正确的方法似乎是:在getDocumentFilter本身上覆盖AbstractFormatter ()

受保护的DocumentFilter getDocumentFilter() 子类和重写,如果您希望提供一个DocumentFilter来限制可以输入的内容。安装将将返回的值安装到JFormattedTextField上。

来自https://docs.oracle.com/javase/7/docs/api/javax/swing/JFormattedTextField.AbstractFormatter.html

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/20074778

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档