I have a JTextPane where I want to add lines and depending on their content have them have a different formatting.
Currently I have this
StyleContext context = new StyleContext();
StyledDocument document = new DefaultStyledDocument(context);
Style styleBold = context.getStyle(StyleContext.DEFAULT_STYLE);
StyleConstants.setBold(styleBold, true);
StyleConstants.setFontSize(styleBold, 18);
Style styleNorm = context.getStyle(StyleContext.DEFAULT_STYLE);
StyleConstants.setFontSize(styleNorm, 15);
for (int i = 0; i < temp.size(); i++) {
String tmp = temp.get(i);
if (tmp.substring(0, 2).equals(COMMENT_PREFIX)) {
String addThis = " - " + tmp.substring(2);
try {
document.insertString(document.getLength(), addThis,
styleNorm);
} //CATCH
} else if (tmp.substring(0, 2).equals(VERSION_PREFIX)) {
Date d = new Date(System.currentTimeMillis());
String addThis = "Version: " + tmp.substring(2) + " - "
+ d.toString();
try {
document.insertString(document.getLength(), addThis,
styleBold);
} //CATCH
}
try {
document.insertString(document.getLength(), "\n", styleNorm);
} //CATCH
}
I took out the catch statements to reduce code size.
However, this formats my entire text with the styleNorm. Is this because it's the last called Style and they overwrite eachother? If so, how do I fix this?
