如何在 JTextField 中设置会计代码文档过滤器
How can I set fiscal code document filter in JTextField
我正在使用 Java swing 程序。我想在我的 JPanel 中插入一个具有限制字符数的 JTextFieldFormat 并将所有文本转换为 UPPER 文本。
所以我构建了这段代码:
textCodiceFiscale = new JTextField();
textCodiceFiscale.setDocument(new PersonalizzaJtextField(numberCharacter));
DocumentFilter filter = new UppercaseDocumentFilter();
((AbstractDocument) textCodiceFiscale.getDocument()).setDocumentFilter(filter);
这是 UppercaseDocumentFilter class:
class UppercaseDocumentFilter extends DocumentFilter {
public void insertString(DocumentFilter.FilterBypass fb, int offset,
String text, AttributeSet attr) throws BadLocationException {
fb.insertString(offset, text.toUpperCase(), attr);
}
public void replace(DocumentFilter.FilterBypass fb, int offset, int length,
String text, AttributeSet attrs) throws BadLocationException {
fb.replace(offset, length, text.toUpperCase(), attrs);
}
}
这是 PersonalizzaJTextField class:
public class PersonalizzaJtextField extends PlainDocument {
//private StringBuffer cache = new StringBuffer();
private int lunghezzaMax;
public PersonalizzaJtextField(int lunghezzaMax){
super();
this.lunghezzaMax = lunghezzaMax;
}
public void insertString(int offset, String str, AttributeSet attr) throws BadLocationException{
if (str == null)
return;
if ((getLength() + str.length()) <= lunghezzaMax) {
super.insertString(offset, str, attr);
}
}
}
现在的问题有两个:
1) 使用此代码,我只能在我的 JTextField 中插入大写字符,但我在第二行限制字符数,但不起作用。
2) 我想为此 JTextField 创建一个模板,我必须在此模式下插入数字或文本:
6 characters 2 numbers 1 character 2 numbers 1 character 3 numbers 1 character.
可以这样做吗?
I want create a template for this JTextField, I must insert number or text in this mode:
使用 JFormattedTextField。您可以使用 MaskFormatter 让 JFormattedTextField 为您进行编辑。使用适当的掩码,您可以指定数字或大写字符。
阅读 How to Use Formatted Text Fields 上的 Swing 教程部分以获取更多信息和示例。
顺便说一句,格式化文本字段会使用 MaskFormatter 自动为您创建 DocumentFilter,因此您需要一个自定义过滤器。
收藏教程。在关于该主题的最后一个问题中,您还获得了教程的 link。使用 Swing 基础教程。
我正在使用 Java swing 程序。我想在我的 JPanel 中插入一个具有限制字符数的 JTextFieldFormat 并将所有文本转换为 UPPER 文本。 所以我构建了这段代码:
textCodiceFiscale = new JTextField();
textCodiceFiscale.setDocument(new PersonalizzaJtextField(numberCharacter));
DocumentFilter filter = new UppercaseDocumentFilter();
((AbstractDocument) textCodiceFiscale.getDocument()).setDocumentFilter(filter);
这是 UppercaseDocumentFilter class:
class UppercaseDocumentFilter extends DocumentFilter {
public void insertString(DocumentFilter.FilterBypass fb, int offset,
String text, AttributeSet attr) throws BadLocationException {
fb.insertString(offset, text.toUpperCase(), attr);
}
public void replace(DocumentFilter.FilterBypass fb, int offset, int length,
String text, AttributeSet attrs) throws BadLocationException {
fb.replace(offset, length, text.toUpperCase(), attrs);
}
}
这是 PersonalizzaJTextField class:
public class PersonalizzaJtextField extends PlainDocument {
//private StringBuffer cache = new StringBuffer();
private int lunghezzaMax;
public PersonalizzaJtextField(int lunghezzaMax){
super();
this.lunghezzaMax = lunghezzaMax;
}
public void insertString(int offset, String str, AttributeSet attr) throws BadLocationException{
if (str == null)
return;
if ((getLength() + str.length()) <= lunghezzaMax) {
super.insertString(offset, str, attr);
}
}
}
现在的问题有两个:
1) 使用此代码,我只能在我的 JTextField 中插入大写字符,但我在第二行限制字符数,但不起作用。
2) 我想为此 JTextField 创建一个模板,我必须在此模式下插入数字或文本:
6 characters 2 numbers 1 character 2 numbers 1 character 3 numbers 1 character.
可以这样做吗?
I want create a template for this JTextField, I must insert number or text in this mode:
使用 JFormattedTextField。您可以使用 MaskFormatter 让 JFormattedTextField 为您进行编辑。使用适当的掩码,您可以指定数字或大写字符。
阅读 How to Use Formatted Text Fields 上的 Swing 教程部分以获取更多信息和示例。
顺便说一句,格式化文本字段会使用 MaskFormatter 自动为您创建 DocumentFilter,因此您需要一个自定义过滤器。
收藏教程。在关于该主题的最后一个问题中,您还获得了教程的 link。使用 Swing 基础教程。