setDocumentFilter
is undefined for the Document
class. You need
DocumentFilter filter = new LengthFilter();
((AbstractDocument)idOneTextField.getDocument()).setDocumentFilter(filter);
Domanda
I'm using BlueJ, and I have two classes.
public class mainMenu extends JFrame
{
ArrayList<String> one=new ArrayList<String>();
ArrayList<String> zero=new ArrayList<String>();
public static void main(String[] args){
mainMenu m=new mainMenu();
}
public mainMenu(){
JPanel p=new JPanel();
JTextField idOne=new JTextField(4);
DocumentFilter fOne=new LengthFilter();
idOne.getDocument().setDocumentFilter(fOne);
}
}
and
class LengthFilter extends DocumentFilter{
private int max=4;
public void insertString(DocumentFilter.FilterBypass fb, int offset,String text, AttributeSet attr) throws BadLocationException {
if (fb.getDocument().getLength() + text.length() <= max){
fb.insertString(offset, text, attr);
}else {
Toolkit.getDefaultToolkit().beep();
}
}
public void replace(DocumentFilter.FilterBypass fb, int offset, int length,
String text, AttributeSet attr) throws BadLocationException {
if (fb.getDocument().getLength() + text.length() - length <= max){
fb.replace(offset, length, text, attr);
}else {
Toolkit.getDefaultToolkit().beep();
}
}
}
When I try to compile the first class, it comes up with a compiler error:
cannot find symbol - method setDocumentFilter(javax.swing.text.DocumentFilter)
Why and how do I fix this?
Soluzione
setDocumentFilter
is undefined for the Document
class. You need
DocumentFilter filter = new LengthFilter();
((AbstractDocument)idOneTextField.getDocument()).setDocumentFilter(filter);