我对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,但是它真的打算那样工作吗?文档应该是处理其过滤器(imho)的对象,而不是格式化程序。有没有办法避免使用专门的格式化程序子类来绕过它?
示例代码:(按请求:)
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使用标志,您不能同时使用,但我一点也不高兴需要一个)
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);
}
}发布于 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
https://stackoverflow.com/questions/20074778
复制相似问题