Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do you set the tab size in a JEditorPane?

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;
}
like image 729
jjnguy Avatar asked Apr 16 '09 19:04

jjnguy


1 Answers

As JEditorPane is designed to support different kinds of content types, it does not 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";
like image 60
Daniel Schneller Avatar answered Oct 01 '22 14:10

Daniel Schneller