0
列/衝突的數目我有一個WWFormattedTextField:限制文本框輸入用的DocumentFilter
public class WWFormattedTextField extends JFormattedTextField implements FocusListener {
private DocumentFilter filter = new UppercaseDocumentFilter();
private boolean limitToMaxInput = false;
public WWFormattedTextField() {
super();
init();
}
private void init() {
addFocusListener(this);
//This line makes ALL text in ALL textfields uppercase as they type
((AbstractDocument) this.getDocument()).setDocumentFilter(filter);
}
//This method uses value of colulmn property of textfield and creates a mask
//that limits the amount of characters
public void setLimitToMaxInput(boolean limitToMaxInput) {
int columns = getColumns();
String patternLimit = "";
while (columns > 0) {
patternLimit = patternLimit.concat("*");
columns -= 1;
}
try {
setFormatterFactory(new javax.swing.text.DefaultFormatterFactory(new javax.swing.text.MaskFormatter(patternLimit)));
} catch (ParseException ex) {
Logger.getLogger(WWFormattedTextField.class.getName()).log(Level.SEVERE, null, ex);
}
this.limitToMaxInput = limitToMaxInput;
}
我的問題是,setLimitToMaxInput覆蓋UppercaseDocumentFilter。該字段限於列數,但由於掩碼是* - 當用戶鍵入時 - 它不會大寫......
需要一些有用的提示或建議以實現我的目標的正確方法:始終當達到最大大小(列)時限制(停止)用戶輸入的upprercase文本字段。
代碼爲UppercaseDocumentFilter:
public class UppercaseDocumentFilter extends DocumentFilter{
@Override
public void insertString(DocumentFilter.FilterBypass fb, int offset, String text, AttributeSet attr) throws BadLocationException {
fb.insertString(offset, text.toUpperCase(), attr);
}
@Override
public void replace(DocumentFilter.FilterBypass fb, int offset, int length, String text, AttributeSet attrs) throws BadLocationException {
fb.replace(offset, length, text.toUpperCase(), attrs);
}
}