Articles Projects Tips Downloads Contacts About

Default TabStop size customization in JEditorPane.

By default java uses 72 pixels TabStop size which is hardcoded in ParagraphView class. To change this nextTabStop() method is changed to provide our custom value. In this example the value is defined by a constant (TAB_SIZE=36 i.e. 0.5 inch) but it can be extended to have custom tab stop size for each paragraph.

The screenshot shows how it looks like:

The example code is below:

import javax.swing.text.*;
import javax.swing.*;
public class TabSizeEditorKit extends StyledEditorKit {

    public static final int TAB_SIZE=36;
    public ViewFactory getViewFactory() {
        return new MyViewFactory();
    static class MyViewFactory implements ViewFactory {

        public View create(Element elem) {
            String kind = elem.getName();
            if (kind != null) {
                if (kind.equals(AbstractDocument.ContentElementName)) {
                    return new LabelView(elem);
                } else if (kind.equals(AbstractDocument.ParagraphElementName)) {
                    return new CustomTabParagraphView(elem);
                } else if (kind.equals(AbstractDocument.SectionElementName)) {
                    return new BoxView(elem, View.Y_AXIS);
                } else if (kind.equals(StyleConstants.ComponentElementName)) {
                    return new ComponentView(elem);
                } else if (kind.equals(StyleConstants.IconElementName)) {
                    return new IconView(elem);
            return new LabelView(elem);
    public static void main(String[] args) {
        JFrame frame=new JFrame("Custom default Tab Size in EditorKit example");
        JEditorPane edit=new JEditorPane();
        edit.setEditorKit(new TabSizeEditorKit());
        try {
            edit.getDocument().insertString(0,"1\t2\t3\t4\t5", new SimpleAttributeSet());
        } catch (BadLocationException e) {
        frame.getContentPane().add(new JScrollPane(edit));

    static class CustomTabParagraphView extends ParagraphView {
        public CustomTabParagraphView(Element elem) {
        public float nextTabStop(float x, int tabOffset) {
            TabSet tabs = getTabSet();
            if(tabs == null) {
                // a tab every 72 pixels.
                return (float)(getTabBase() + (((int)x / TAB_SIZE + 1) * TAB_SIZE));
            return super.nextTabStop(x, tabOffset);