The list of methods to do JTextPane are organized into topic(s).
void
appendAsGreen(JTextPane pane, String fontName, int fontSize, String s) append As Green
if (pane.getStyle("greenStyle") == null) {
Style greenStyle = pane.addStyle("greenStyle", null);
StyleConstants.setForeground(greenStyle, SUCCESSFUL_GREEN);
StyleConstants.setFontFamily(greenStyle, fontName);
StyleConstants.setFontSize(greenStyle, fontSize);
StyledDocument doc = (StyledDocument) pane.getDocument();
try {
...
void
appendAsMessage(JTextPane pane, String fontName, int fontSize, String s) Appends a given string to the StyledDocument of a pane as a
message, that is, using the Style 'MessageStyle'.
if (pane.getStyle("MessageStyle") == null) {
Style messageStyle = pane.addStyle("MessageStyle", null);
StyleConstants.setForeground(messageStyle, Color.BLACK);
StyleConstants.setFontFamily(messageStyle, fontName);
StyleConstants.setFontSize(messageStyle, fontSize);
StyledDocument doc = (StyledDocument) pane.getDocument();
try {
...
void
appendToPane(JTextPane textPane, String text, Color foregroundColor, Color backgroundColor) Append some text in one JTextPane, using colors.
StyleContext sc = StyleContext.getDefaultStyleContext();
AttributeSet aset = sc.addAttribute(SimpleAttributeSet.EMPTY, StyleConstants.Foreground, foregroundColor);
aset = sc.addAttribute(aset, StyleConstants.Background, backgroundColor);
int len = textPane.getDocument().getLength();
textPane.setCaretPosition(len);
textPane.setCharacterAttributes(aset, false);
textPane.replaceSelection(text);
void
applyRegex(String regex, JTextPane pane, Color highlightColor) apply Regex
Pattern pattern = Pattern.compile(regex);
Matcher matcher = pattern.matcher(pane.getText());
while (matcher.find()) {
StyleContext sc = StyleContext.getDefaultStyleContext();
AttributeSet aset = sc.addAttribute(SimpleAttributeSet.EMPTY, StyleConstants.Foreground,
highlightColor);
pane.getStyledDocument().setCharacterAttributes(matcher.start(), matcher.end() - matcher.start(), aset,
true);
...
void
centerText(JTextPane pane) center Text
StyledDocument doc = pane.getStyledDocument();
SimpleAttributeSet center = new SimpleAttributeSet();
StyleConstants.setAlignment(center, StyleConstants.ALIGN_CENTER);
doc.setParagraphAttributes(0, doc.getLength(), center, false);
JTextPane
createJTextPane(String text, Color backgroundColor) Creates a new
JTextPane object with the given properties.
JTextPane jTextPane = new JTextPane();
jTextPane.setBorder(null);
jTextPane.setEditable(false);
jTextPane.setBackground(backgroundColor);
jTextPane.setFont(new Font("Times New Roman", Font.PLAIN, 14));
if (text != null) {
jTextPane.setText(text);
jTextPane.setVerifyInputWhenFocusTarget(false);
jTextPane.setAutoscrolls(false);
return jTextPane;
JScrollPane
createTextPaneScrollPane(JTextPane textPane) Creates a default scroll pane for a text pane.
JPanel noWrapPanel = new JPanel(new BorderLayout());
noWrapPanel.add(textPane);
JScrollPane scrollPane = new JScrollPane(noWrapPanel);
scrollPane.getVerticalScrollBar().setUnitIncrement(textPane.getFont().getSize() * 2);
return scrollPane;
String
getFontFamily(JTextPane textPane) Returns the font family of the current character.
String family = (String) (textPane.getInputAttributes().getAttribute(StyleConstants.FontFamily));
return family;