APIdia Launch Partner
Top Description Inners Constructors Methods
javax.swing.text

public Class DocumentFilter

extends Object
Class Inheritance
Known Direct Subclasses
javax.swing.JSpinner.ListEditor.ListFormatter.Filter, javax.swing.text.DefaultFormatter.DefaultDocumentFilter

DocumentFilter, as the name implies, is a filter for the Document mutation methods. When a Document containing a DocumentFilter is modified (either through insert or remove), it forwards the appropriate method invocation to the DocumentFilter. The default implementation allows the modification to occur. Subclasses can filter the modifications by conditionally invoking methods on the superclass, or invoking the necessary methods on the passed in FilterBypass. Subclasses should NOT call back into the Document for the modification instead call into the superclass or the FilterBypass.

When remove or insertString is invoked on the DocumentFilter, the DocumentFilter may callback into the FilterBypass multiple times, or for different regions, but it should not callback into the FilterBypass after returning from the remove or insertString method.

By default, text related document mutation methods such as insertString, replace and remove in AbstractDocument use DocumentFilter when available, and Element related mutation methods such as create, insert and removeElement in DefaultStyledDocument do not use DocumentFilter. If a method doesn't follow these defaults, this must be explicitly stated in the method documentation.

Since
1.4
See Also
javax.swing.text.Document, javax.swing.text.AbstractDocument, javax.swing.text.DefaultStyledDocument

Nested and Inner Type Summary

Modifier and TypeClass and Description
public abstract static class
DocumentFilter.FilterBypass

Used as a way to circumvent calling back into the Document to change it.

Constructor Summary

AccessConstructor and Description
public
DocumentFilter()

Constructs a DocumentFilter.

Method Summary

Modifier and TypeMethod and Description
public void
insertString(DocumentFilter.FilterBypass
FilterBypass that can be used to mutate Document
fb
,
int
the offset into the document to insert the content >= 0. All positions that track change at or after the given location will move.
offset
,
String
the string to insert
string
,
AttributeSet
the attributes to associate with the inserted content. This may be null if there are no attributes.
attr
)

Invoked prior to insertion of text into the specified Document.

public void
remove(DocumentFilter.FilterBypass
FilterBypass that can be used to mutate Document
fb
,
int
the offset from the beginning >= 0
offset
,
int
the number of characters to remove >= 0
length
)

Invoked prior to removal of the specified region in the specified Document.

public void
replace(DocumentFilter.FilterBypass
FilterBypass that can be used to mutate Document
fb
,
int
Location in Document
offset
,
int
Length of text to delete
length
,
String
Text to insert, null indicates no text to insert
text
,
AttributeSet
AttributeSet indicating attributes of inserted text, null is legal.
attrs
)

Invoked prior to replacing a region of text in the specified Document.

Inherited from java.lang.Object:
cloneequalsfinalizegetClasshashCodenotifynotifyAlltoStringwaitwaitwait

Constructor Detail

DocumentFilterback to summary
public DocumentFilter()

Constructs a DocumentFilter.

Method Detail

insertStringback to summary
public void insertString(DocumentFilter.FilterBypass fb, int offset, String string, AttributeSet attr) throws BadLocationException

Invoked prior to insertion of text into the specified Document. Subclasses that want to conditionally allow insertion should override this and only call supers implementation as necessary, or call directly into the FilterBypass.

Parameters
fb:DocumentFilter.FilterBypass

FilterBypass that can be used to mutate Document

offset:int

the offset into the document to insert the content >= 0. All positions that track change at or after the given location will move.

string:String

the string to insert

attr:AttributeSet

the attributes to associate with the inserted content. This may be null if there are no attributes.

Exceptions
BadLocationException:
the given insert position is not a valid position within the document
removeback to summary
public void remove(DocumentFilter.FilterBypass fb, int offset, int length) throws BadLocationException

Invoked prior to removal of the specified region in the specified Document. Subclasses that want to conditionally allow removal should override this and only call supers implementation as necessary, or call directly into the FilterBypass as necessary.

Parameters
fb:DocumentFilter.FilterBypass

FilterBypass that can be used to mutate Document

offset:int

the offset from the beginning >= 0

length:int

the number of characters to remove >= 0

Exceptions
BadLocationException:
some portion of the removal range was not a valid part of the document. The location in the exception is the first bad position encountered.
replaceback to summary
public void replace(DocumentFilter.FilterBypass fb, int offset, int length, String text, AttributeSet attrs) throws BadLocationException

Invoked prior to replacing a region of text in the specified Document. Subclasses that want to conditionally allow replace should override this and only call supers implementation as necessary, or call directly into the FilterBypass.

Parameters
fb:DocumentFilter.FilterBypass

FilterBypass that can be used to mutate Document

offset:int

Location in Document

length:int

Length of text to delete

text:String

Text to insert, null indicates no text to insert

attrs:AttributeSet

AttributeSet indicating attributes of inserted text, null is legal.

Exceptions
BadLocationException:
the given insert position is not a valid position within the document
javax.swing.text back to summary

public abstract Class DocumentFilter.FilterBypass

extends Object
Class Inheritance
Known Direct Subclasses
javax.swing.text.AbstractDocument.DefaultFilterBypass

Used as a way to circumvent calling back into the Document to change it. Document implementations that wish to support a DocumentFilter must provide an implementation that will not callback into the DocumentFilter when the following methods are invoked from the DocumentFilter.
Since
1.4

Constructor Summary

AccessConstructor and Description
protected
FilterBypass()

Constructor for subclasses to call.

Method Summary

Modifier and TypeMethod and Description
public abstract Document

Returns:

Document that remove/insertString will operate on
getDocument
()

Returns the Document the mutation is occurring on.

public abstract void
insertString(int
the offset into the document to insert the content >= 0. All positions that track change at or after the given location will move.
offset
,
String
the string to insert
string
,
AttributeSet
the attributes to associate with the inserted content. This may be null if there are no attributes.
attr
)

Inserts the specified text, bypassing the DocumentFilter.

public abstract void
remove(int
the offset from the beginning >= 0
offset
,
int
the number of characters to remove >= 0
length
)

Removes the specified region of text, bypassing the DocumentFilter.

public abstract void
replace(int
Location in Document
offset
,
int
Length of text to delete
length
,
String
Text to insert, null indicates no text to insert
string
,
AttributeSet
AttributeSet indicating attributes of inserted text, null is legal.
attrs
)

Deletes the region of text from offset to offset + length, and replaces it with text.

Inherited from java.lang.Object:
cloneequalsfinalizegetClasshashCodenotifynotifyAlltoStringwaitwaitwait

Constructor Detail

FilterBypassback to summary
protected FilterBypass()

Constructor for subclasses to call.

Method Detail

getDocumentback to summary
public abstract Document getDocument()

Returns the Document the mutation is occurring on.

Returns:Document

Document that remove/insertString will operate on

insertStringback to summary
public abstract void insertString(int offset, String string, AttributeSet attr) throws BadLocationException

Inserts the specified text, bypassing the DocumentFilter.

Parameters
offset:int

the offset into the document to insert the content >= 0. All positions that track change at or after the given location will move.

string:String

the string to insert

attr:AttributeSet

the attributes to associate with the inserted content. This may be null if there are no attributes.

Exceptions
BadLocationException:
the given insert position is not a valid position within the document
removeback to summary
public abstract void remove(int offset, int length) throws BadLocationException

Removes the specified region of text, bypassing the DocumentFilter.

Parameters
offset:int

the offset from the beginning >= 0

length:int

the number of characters to remove >= 0

Exceptions
BadLocationException:
some portion of the removal range was not a valid part of the document. The location in the exception is the first bad position encountered.
replaceback to summary
public abstract void replace(int offset, int length, String string, AttributeSet attrs) throws BadLocationException

Deletes the region of text from offset to offset + length, and replaces it with text.

Parameters
offset:int

Location in Document

length:int

Length of text to delete

string:String

Text to insert, null indicates no text to insert

attrs:AttributeSet

AttributeSet indicating attributes of inserted text, null is legal.

Exceptions
BadLocationException:
the given insert is not a valid position within the document