Java JFormattedTextField破坏DocumentFilter
我对JFormattedTextField有一个问题(我将它用作所有文本字段的基类) 今天,我尝试向该字段的文档添加一个文档过滤器,该过滤器工作正常,但前提是它没有格式化程序工厂设置 问题是,当设置格式化程序工厂(在我的例子中是默认类)并调用processFocusEvent时,会发生以下情况(JFormattedTextField.java:595): 然后调用setValue()(JFormattedTextField.java:757): 如您所见,如果有工厂,它将尝试“刷新”格式化程序 (JFormattedTextField.java:439): 这是我遇到的真正问题(JFormattedTextField$AbstractFormatter.class:950): 在这里,它破坏了我的文档过滤器,我知道格式化程序通常持有文档过滤器,但它真的打算这样工作吗? 文档应该是处理其筛选器(imho)的对象,而不是格式化程序。 有没有一种方法可以绕过它而不使用专门的格式化程序子类 示例代码: (按要求:) 这两个文本字段应该具有相同的文档筛选器,但键入其中一个字段时,它将始终获得大写字母,而另一个字段则不会 当前解决方案: (我不久前编写的解决方案是在JFormattedTextField的子类中实现的,我需要使用该标志,以防格式化程序也有documentfilter,您不能同时使用这两个,但我真的不太乐意需要一个)Java JFormattedTextField破坏DocumentFilter,java,swing,formatting,jformattedtextfield,documentfilter,Java,Swing,Formatting,Jformattedtextfield,Documentfilter,我对JFormattedTextField有一个问题(我将它用作所有文本字段的基类) 今天,我尝试向该字段的文档添加一个文档过滤器,该过滤器工作正常,但前提是它没有格式化程序工厂设置 问题是,当设置格式化程序工厂(在我的例子中是默认类)并调用processFocusEvent时,会发生以下情况(JFormattedTextField.java:595): 然后调用setValue()(JFormattedTextField.java:757): 如您所见,如果有工厂,它将尝试“刷新”格式化程序
public boolean isPreserveDocumentFilter(){
返回过滤器;
}
公共void setPreserveDocumentFilter(布尔值preserveDocumentFilter){
this.preserveDocumentFilter=preserveDocumentFilter;
}
/**
*如果我们想在DefaultFormatter实现中使用documentFilter,我们需要重写。
*有关更多信息,请参阅:
*/
@凌驾
受保护的void setFormatter(AbstractFormatter格式){
Document doc=this.getDocument();
DocumentFilter=null;
如果(保存文档过滤器){
if(抽象文档的文档实例){
filter=((AbstractDocument)doc.getDocumentFilter();
}
}
super.setFormatter(格式);
if(过滤器!=null){
((AbstractDocument)doc.setDocumentFilter(filter);
}
}
我也面临同样的问题。基本上,正确的方法似乎是:在AbstractFormatter本身上重写getDocumentFilter()
受保护的DocumentFilter getDocumentFilter()
如果希望为提供DocumentFilter,请创建子类并重写
限制可以输入的内容。install将安装返回的值
进入JFormattedTextField
from请从代码开始,代码采用SSCCE格式,简短、可运行、可编译,大约是JFrame,带有一个JFormattedTextField和您的DocumentFilter租约(没有指定格式化程序、inputmask、验证程序,只有我的快照)为什么有DocumentFilter,原因是什么,例如,您可以使用NavigationFilter而不是DocumentFilter,但coint总是有两面性的,确实需要使用DocumentFilter for JSpinner和NumberPinnerModel的NumberFormatter,否则您可以将任何字符输入编辑器,SSCCE wokrs me正确(包括特殊字符和本地化键盘)对于Win8/Java7上的CP1250,请询问您如何覆盖子类JFormattedTextField的自定义信号量,可能有…,但我不想猜测,不使用这些类的需要是由我工作的环境给出的,我在这方面无法真正争论。据我所知,这是一种流行的方法(DocFilter)来实现示例中的大写过滤器之类的东西,并且与JTextField配合使用效果很好,因此JFormattedTextField打破了它(可以这么说)。顺便说一句,我的环境是Win7 x86,Sun JDK 1.631。最新的jdk6和jdk7之间没有变化,请参阅我的测试代码以了解JTextComponents的各种情况
// 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);
}
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);
}
}
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);
}
public void uninstall() {
if (this.ftf != null) {
installDocumentFilter(null);
this.ftf.setNavigationFilter(null);
this.ftf.setFormatterActions(null);
}
}
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);
}
}
}
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);
}
}