I’m creating a custom DocumentFilter.
However, I have to use it on several different components. Only difference between them is character limit, which could be changed by changing a single variable.
The question is, how do I pass that variable to DocumentFilter?
This is my custom DocumentFilter class (most of the code removed):
class DefaultDocFilter extends DocumentFilter
{
public void insertString(FilterBypass fb, int offs,
String str, AttributeSet a)
{
//do something with charLimit
}
public void replace(FilterBypass fb, int offs, int length,
String str, AttributeSet a)
{
//do something else with charLimit
}
}
Implementation in main code:
int charLimit = 40;
doc = (AbstractDocument) JTextArea.getDocument();
doc.setDocumentFilter(new DefaultDocFilter());
How do I pass charLimit to the DefaultDocFilter?
You could simply add as member variable:
then: