gpt4 book ai didi

java - JFormattedTextField 破坏 DocumentFilter

转载 作者:行者123 更新时间:2023-11-29 08:56:31 28 4
gpt4 key购买 nike

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

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

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

    // 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):

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):

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):

    public void uninstall() {
if (this.ftf != null) {
installDocumentFilter(null);
this.ftf.setNavigationFilter(null);
this.ftf.setFormatterActions(null);
}
}

这里它破坏了我的文档过滤器,我知道格式化程序通常持有 documentFilter,但它真的打算那样工作吗?该文档应该是处理其过滤器(恕我直言)而不是格式化程序的对象。有没有办法在不使用专门的格式化程序子类的情况下绕过它?

示例代码:(按要求:))

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 的子类中实现的,我需要在格式化程序也有 documentfilter 的情况下使用标志,你不能​​同时使用两者,但我不是很高兴需要一个完全没有)

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

最佳答案

我遇到了同样的问题。基本上,正确的方法似乎是:覆盖 AbstractFormatter 本身的 getDocumentFilter()

protected DocumentFilter getDocumentFilter()

Subclass and override if you wish to provide a DocumentFilter to restrict what can be input. install will install the returned value onto the JFormattedTextField.

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

关于java - JFormattedTextField 破坏 DocumentFilter,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20074778/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com