views:

639

answers:

2

A JTextArea's tab size can easily be set using setTabSize(int).

Is there a similar way to do it with a JEditorPane?

Right now, text with tabs in my pane looks like:

if (stuff){
            more stuff;
}

And, I'd prefer a much smaller tab stop:

if (stuff){
    more stuff;
}
+3  A: 

As JEditorPane is designed to support different kinds of content types, it does provide a way to specify a "tab size" directly, because the meaning of that should be defined by the content model. However when you use a model that's a PlainDocument or one of its descendants, there is a "tabSizeAttribute" that provides what you are looking for.

Example:

JEditorPane pane = new JEditorPane(...);
...
Document doc = pane.getDocument();
if (doc instanceof PlainDocument) {
    doc.putProperty(PlainDocument.tabSizeAttribute, 8);
}
...

From the Javadoc:

/**
 * Name of the attribute that specifies the tab
 * size for tabs contained in the content.  The
 * type for the value is Integer.
 */
public static final String tabSizeAttribute = "tabSize";
Daniel Schneller
Thanks for not just a 'how', but a 'why' too!
jjnguy
+1  A: 

Daniel Schneller already has the answer for if your JEditorPane is using a PlainDocument.
If you're using a StyledDocument instead, this code should help.

Michael Myers
Bah, the other answer was accepted while I wrote this one. I guess that answers the question of which type of document.
Michael Myers
I'm basically using the JEditorPane as a JTextArea right now. Extra functionality will be added later. Thanks for the extra info.
jjnguy