Raekye
Raekye

Reputation: 5131

Java add global JTextPane styles/attributes?

I want to add a global AttributeSet to my JTextPane.

I found this:

SimpleAttributeSet style = new SimpleAttributeSet();
StyleConstants.setLeftIndent(style, 20);
StyleConstants.setFirstLineIndent(style, -20);

From http://java-sl.com/tip_hanging_first_line.html

I'm wondering how I can set the "default stylesheet"? (not using HTML). I then tried this:

StyleContext style = new StyleContext();
Style s = style.addStyle("test", null);
StyleConstants.setForeground(s, Color.BLUE);
StyledDocument d = (StyledDocument) console.getOutputField().getDocument();

From http://www.java2s.com/Code/Java/Swing-JFC/JTextPaneStylesExample1.htm without luck.

I know StyledDocument has specific properties for setting stuff like foreground colour - which is why this may not work - but can anyone point me as to how to use the other style attributes? Such as the left indent and first line indent.

Upvotes: 1

Views: 1663

Answers (1)

camickr
camickr

Reputation: 324098

JTextPane textPane = new JTextPane();
StyledDocument doc = textPane.getStyledDocument();
SimpleAttributeSet style = new SimpleAttributeSet();
StyleConstants.setLeftIndent(style, 20);
StyleConstants.setFirstLineIndent(style, -20);
StyleConstants.setForeground(style, Color.BLUE);
doc.setParagraphAttributes(0, doc.getLength(), style, true);

Upvotes: 3

Related Questions