用于大写的Java Swing DocumentFilter
问题是当我选择文本并重新键入时;然后选定的文本不会被删除,但会保留(请查看顶部的gif) 您应该调用用于大写的Java Swing DocumentFilter,java,swing,Java,Swing,问题是当我选择文本并重新键入时;然后选定的文本不会被删除,但会保留(请查看顶部的gif) 您应该调用super.replace(…),因为您正在重写replace()方法 您应该调用super.replace(…),因为您正在重写replace()方法 自定义文本字段 import java.awt.KeyboardFocusManager; import javax.swing.InputVerifier; import javax.swing.JTextField; import javax
super.replace(…)
,因为您正在重写replace()方法
您应该调用super.replace(…)
,因为您正在重写replace()方法
自定义文本字段
import java.awt.KeyboardFocusManager;
import javax.swing.InputVerifier;
import javax.swing.JTextField;
import javax.swing.text.AbstractDocument;
import javax.swing.text.AttributeSet;
import javax.swing.text.BadLocationException;
import javax.swing.text.DocumentFilter;
import javax.swing.text.DocumentFilter.FilterBypass;
/**
*
* @author Igor
*/
public class CustomLengthTextField extends JTextField {
protected boolean upper = false;
protected int maxlength = 0;
public CustomLengthTextField() {
this(-1);
}
public CustomLengthTextField(int length, boolean upper) {
this(length, upper, null);
}
public CustomLengthTextField(int length, InputVerifier inpVer) {
this(length, false, inpVer);
}
/**
*
* @param length - maksimalan length
* @param upper - turn it to upercase
* @param inpVer - InputVerifier
*/
public CustomLengthTextField(int length, boolean upper, InputVerifier inpVer) {
super();
this.maxlength = length;
this.upper = upper;
if (length > 0) {
AbstractDocument doc = (AbstractDocument) getDocument();
doc.setDocumentFilter(new DocumentSizeFilter());
}
setInputVerifier(inpVer);
}
public CustomLengthTextField(int length) {
this(length, false);
}
public void setMaxLength(int length) {
this.maxlength = length;
}
class DocumentSizeFilter extends DocumentFilter {
public void insertString(FilterBypass fb, int offs, String str, AttributeSet a)
throws BadLocationException {
//This rejects the entire insertion if it would make
//the contents too long. Another option would be
//to truncate the inserted string so the contents
//would be exactly maxCharacters in length.
if ((fb.getDocument().getLength() + str.length()) <= maxlength) {
super.insertString(fb, offs, str, a);
}
}
public void replace(FilterBypass fb, int offs,
int length,
String str, AttributeSet a)
throws BadLocationException {
if (upper) {
str = str.toUpperCase();
}
//This rejects the entire replacement if it would make
//the contents too long. Another option would be
//to truncate the replacement string so the contents
//would be exactly maxCharacters in length.
int charLength = fb.getDocument().getLength() + str.length() - length;
if (charLength <= maxlength) {
super.replace(fb, offs, length, str, a);
if (charLength == maxlength) {
focusNextComponent();
}
} else {
focusNextComponent();
}
}
private void focusNextComponent() {
if (CustomLengthTextField.this == KeyboardFocusManager.getCurrentKeyboardFocusManager().getFocusOwner()) {
KeyboardFocusManager.getCurrentKeyboardFocusManager().focusNextComponent();
}
}
}
}
导入java.awt.KeyboardFocusManager;
导入javax.swing.InputVerifier;
导入javax.swing.JTextField;
导入javax.swing.text.AbstractDocument;
导入javax.swing.text.AttributeSet;
导入javax.swing.text.BadLocationException;
导入javax.swing.text.DocumentFilter;
导入javax.swing.text.DocumentFilter.FilterBypass;
/**
*
*@作者伊戈尔
*/
公共类CustomLengthTextField扩展了JTextField{
受保护布尔值上限=false;
受保护的整数maxlength=0;
公共CustomLengthTextField(){
这是(-1);
}
公共CustomLengthTextField(整数长度,布尔上限){
该值(长度、上限、空值);
}
公共CustomLengthTextField(int-length,InputVerifier-Inputver){
这(长度、错误、输入);
}
/**
*
*@param-length-maksimalan长度
*@param upper-将其转为大写
*@param Inputver-InputVerifier
*/
公共CustomLengthTextField(整数长度、布尔值上限、InputVerifier Inputver){
超级();
this.maxlength=长度;
this.upper=upper;
如果(长度>0){
AbstractDocument doc=(AbstractDocument)getDocument();
doc.setDocumentFilter(新DocumentSizeFilter());
}
setInputVerifier(输入验证程序);
}
公共CustomLengthTextField(整数长度){
这个(长度,假);
}
公共void setMaxLength(整数长度){
this.maxlength=长度;
}
类DocumentSizeFilter扩展了DocumentFilter{
public void insertString(FilterBypass fb、int offs、String str、AttributeSet a)
抛出BadLocationException{
//这将拒绝整个插入,如果它将
//内容太长。另一个选项是
//截断插入的字符串以便
//长度正好是maxCharacters。
如果((fb.getDocument().getLength()+str.length())
自定义文本字段
import java.awt.KeyboardFocusManager;
import javax.swing.InputVerifier;
import javax.swing.JTextField;
import javax.swing.text.AbstractDocument;
import javax.swing.text.AttributeSet;
import javax.swing.text.BadLocationException;
import javax.swing.text.DocumentFilter;
import javax.swing.text.DocumentFilter.FilterBypass;
/**
*
* @author Igor
*/
public class CustomLengthTextField extends JTextField {
protected boolean upper = false;
protected int maxlength = 0;
public CustomLengthTextField() {
this(-1);
}
public CustomLengthTextField(int length, boolean upper) {
this(length, upper, null);
}
public CustomLengthTextField(int length, InputVerifier inpVer) {
this(length, false, inpVer);
}
/**
*
* @param length - maksimalan length
* @param upper - turn it to upercase
* @param inpVer - InputVerifier
*/
public CustomLengthTextField(int length, boolean upper, InputVerifier inpVer) {
super();
this.maxlength = length;
this.upper = upper;
if (length > 0) {
AbstractDocument doc = (AbstractDocument) getDocument();
doc.setDocumentFilter(new DocumentSizeFilter());
}
setInputVerifier(inpVer);
}
public CustomLengthTextField(int length) {
this(length, false);
}
public void setMaxLength(int length) {
this.maxlength = length;
}
class DocumentSizeFilter extends DocumentFilter {
public void insertString(FilterBypass fb, int offs, String str, AttributeSet a)
throws BadLocationException {
//This rejects the entire insertion if it would make
//the contents too long. Another option would be
//to truncate the inserted string so the contents
//would be exactly maxCharacters in length.
if ((fb.getDocument().getLength() + str.length()) <= maxlength) {
super.insertString(fb, offs, str, a);
}
}
public void replace(FilterBypass fb, int offs,
int length,
String str, AttributeSet a)
throws BadLocationException {
if (upper) {
str = str.toUpperCase();
}
//This rejects the entire replacement if it would make
//the contents too long. Another option would be
//to truncate the replacement string so the contents
//would be exactly maxCharacters in length.
int charLength = fb.getDocument().getLength() + str.length() - length;
if (charLength <= maxlength) {
super.replace(fb, offs, length, str, a);
if (charLength == maxlength) {
focusNextComponent();
}
} else {
focusNextComponent();
}
}
private void focusNextComponent() {
if (CustomLengthTextField.this == KeyboardFocusManager.getCurrentKeyboardFocusManager().getFocusOwner()) {
KeyboardFocusManager.getCurrentKeyboardFocusManager().focusNextComponent();
}
}
}
}
导入java.awt.KeyboardFocusManager;
导入javax.swing.InputVerifier;
导入javax.swing.JTextField;
导入javax.swing.text.AbstractDocument;
导入javax.swing.text.AttributeSet;
导入javax.swing.text.BadLocationException;
导入javax.swing.text.DocumentFilter;
导入javax.swing.text.DocumentFilter.FilterBypass;
/**
*
*@作者伊戈尔
*/
公共类CustomLengthTextField扩展了JTextField{
受保护布尔值上限=false;
受保护的整数maxlength=0;
公共CustomLengthTextField(){
这是(-1);
}
公共CustomLengthTextField(整数长度,布尔上限){
该值(长度、上限、空值);
}
公共CustomLengthTextField(int-length,InputVerifier-Inputver){
这(长度、错误、输入);
}
/**
*
*@param-length-maksimalan长度
*@param upper-将其转为大写
*@param Inputver-InputVerifier
*/
公共CustomLengthTextField(整数长度、布尔值上限、InputVerifier Inputver){
超级();
this.maxlength=长度;
this.upper=upper;
如果(长度>0){
AbstractDocument doc=(AbstractDocument)getDocument();
doc.setDocumentFilter(新DocumentSizeFilter());
}
setInputVerifier(输入验证程序);
}
公共CustomLengthTextField(整数长度){
这个(长度,假);
}
公共void setMaxLength(整数长度){
this.maxlength=长度;
}
类DocumentSizeFilter扩展了DocumentFilter{
public void insertString(FilterBypass fb、int offs、String str、AttributeSet a)
抛出BadLocationException{
//这将拒绝整个插入,如果它将
//内容太长。另一个选项是
//截断插入的字符串以便
//长度正好是maxCharacters。
如果((fb.getDocument().getLength()+str.length())请你发布正确的代码。非常感谢。非常感谢,现在我已经解决了问题谢谢!你太棒了:-)请你发布正确的代码。非常感谢。非常感谢,现在我已经解决了问题谢谢!你太棒了:-)
CustomLengthTextField textField = new CustomLengthTextField(-1, true);
import java.awt.KeyboardFocusManager;
import javax.swing.InputVerifier;
import javax.swing.JTextField;
import javax.swing.text.AbstractDocument;
import javax.swing.text.AttributeSet;
import javax.swing.text.BadLocationException;
import javax.swing.text.DocumentFilter;
import javax.swing.text.DocumentFilter.FilterBypass;
/**
*
* @author Igor
*/
public class CustomLengthTextField extends JTextField {
protected boolean upper = false;
protected int maxlength = 0;
public CustomLengthTextField() {
this(-1);
}
public CustomLengthTextField(int length, boolean upper) {
this(length, upper, null);
}
public CustomLengthTextField(int length, InputVerifier inpVer) {
this(length, false, inpVer);
}
/**
*
* @param length - maksimalan length
* @param upper - turn it to upercase
* @param inpVer - InputVerifier
*/
public CustomLengthTextField(int length, boolean upper, InputVerifier inpVer) {
super();
this.maxlength = length;
this.upper = upper;
if (length > 0) {
AbstractDocument doc = (AbstractDocument) getDocument();
doc.setDocumentFilter(new DocumentSizeFilter());
}
setInputVerifier(inpVer);
}
public CustomLengthTextField(int length) {
this(length, false);
}
public void setMaxLength(int length) {
this.maxlength = length;
}
class DocumentSizeFilter extends DocumentFilter {
public void insertString(FilterBypass fb, int offs, String str, AttributeSet a)
throws BadLocationException {
//This rejects the entire insertion if it would make
//the contents too long. Another option would be
//to truncate the inserted string so the contents
//would be exactly maxCharacters in length.
if ((fb.getDocument().getLength() + str.length()) <= maxlength) {
super.insertString(fb, offs, str, a);
}
}
public void replace(FilterBypass fb, int offs,
int length,
String str, AttributeSet a)
throws BadLocationException {
if (upper) {
str = str.toUpperCase();
}
//This rejects the entire replacement if it would make
//the contents too long. Another option would be
//to truncate the replacement string so the contents
//would be exactly maxCharacters in length.
int charLength = fb.getDocument().getLength() + str.length() - length;
if (charLength <= maxlength) {
super.replace(fb, offs, length, str, a);
if (charLength == maxlength) {
focusNextComponent();
}
} else {
focusNextComponent();
}
}
private void focusNextComponent() {
if (CustomLengthTextField.this == KeyboardFocusManager.getCurrentKeyboardFocusManager().getFocusOwner()) {
KeyboardFocusManager.getCurrentKeyboardFocusManager().focusNextComponent();
}
}
}
}