How to better use JTextPane (1)
I often see many online friends ask how to JTextArea control characters, how to set up specific character colour, and so on. I use Java to be a SQL query analyzer found a better solution is to use JTextPane, then how to better use JTextPane it, I extracted from my part of part of the process, for your reference.
Package com.JDAGUI;
Import javax.swing.text .*;
Import java.util .*;
Import java.awt .*;
Import com.JDA .*;
/ **
* @ Author whxu
* /
Public class JDAStyledDocument extends DefaultStyledDocument
(
Private int type = -1; / / data connection type AttributeSet myAttributeSet = null;
Public JDAStyledDocument (int type)
(
This.type = type;
)
/ **
* Insert the string * /
Public void insertString (int offset, String str, AttributeSet a)
Throws BadLocationException
(
This.myAttributeSet = a;
Super.insertString (offset, str, a);
SetSyntaxColor (offset, str.length ());
)
/ **
* Remove the string * /
Public void remove (int offs, int len)
Throws BadLocationException
(
Super.remove (offs, len);
SetSyntaxColor (offs);
)
/ **
* Access to the location of developed characters * /
Private String getPositionChar (int offset)
(
String str = "";
Try
(
Str = getText (offset, 1);
)
Catch (BadLocationException ex)
(
/ / Ex.printStackTrace (System.out);
)
Return str;
)
/ **
* From the specified location, inverted pushed to the first encounter spaces position * /
Private String getBeforeBlankString (int offset)
(
String str = "";
If (offset <0) return "";
Str = getPositionChar (offset);
If (SyntaxMgr.isSpaceChar (str))
Return "";
String r = getBeforeBlankString (offset-1);
Return r + str;
)
/ **
* From the specified location, with a push to have space here * /
Private String getAfterBlankString (int offset)
(
String str = "";
If (offset> getLength ()) return "";
Str = getPositionChar (offset);
If (SyntaxMgr.isSpaceChar (str))
Return "";
String r = getAfterBlankString (offset +1);
Return str + r;
)
/ **
* Under Postion forward judge, back judge, the use of coloured and returned to the set position at the end of the color * /
Private int setSyntaxColor (int offset)
(
If (offset <0) return offset; / / If the installation position does not exist, do not have to consider if (myAttributeSet == null) return offset; / / If myAttributeSet as null, it is not necessary to consider String ifSyntax = "";
String before getBeforeBlankString = (offset-1);
String after getAfterBlankString = (offset);
Syntax = (before + after). Trim ();
Int start = offset-before.length ();
Int tmp_len = ifSyntax.length ();
If (start <0 | | tmp_len <= 0) return offset; / / If the use of coloured string is empty, the return / / Set the color StyleConstants.setForeground ((MutableAttributeSet) myAttributeSet,
SyntaxMgr.isSyntax (type, ifSyntax));
SetCharacterAttributes (start, tmp_len, myAttributeSet, true);
+ Tmp_len return start;
)
/ **
* According to a scope of the settings within the scope of the SyntaxColor
* /
Private int setSyntaxColor (int offset, int len)
Throws BadLocationException
(
/ / If the scope does not exist, do not consider if (offset <0 | | len <0) return offset;
Int tmp_offset = offset;
While (tmp_offset <offset + len)
(
Tmp_offset = setSyntaxColor (tmp_offset);
Tmp_offset = getNextWordOffset (tmp_offset);
)
Tmp_offset = setSyntaxColor (tmp_offset) / / set up after the last cycle of a return tmp_offset word;
)
/ **
* Under Postion, access to a word starting point * /
Private int getNextWordOffset (int offset)
(
Int rOffset = offset;
Int textlength = getLength ();
While (rOffset <= textlength & offset> = 0)
(
String str = getPositionChar (rOffset);
If (! SyntaxMgr.isSpaceChar (str))
(
Break;
)
ROffset + = 1;
)
If (rOffset! = Offset) / / (set spacing of color
/ / Set the color StyleConstants.setForeground ((MutableAttributeSet) myAttributeSet,
SyntaxColorMgr.getSpaceColor ());
SetCharacterAttributes (offset, rOffset-offset, myAttributeSet, true);
)
Return rOffset;
)
)
To this end here, we do a good job in a JTextPane apply to the Document. JTextPane use relatively simple.






