Search code examples
jtextpanetab-size

Better way of setting tab size in a JTextPane


I was wondering if there is a better way of setting tab size?

FontMetrics fm = textPane.getFontMetrics(textPane.getFont()); // deprecated!
int cw = fm.stringWidth("    ");
float f = (float) cw;
TabStop[] tabs = new TabStop[50]; // this sucks

for (int i = 0; i < tabs.length; i++) {
    tabs[i] = new TabStop(f * (i + 1), TabStop.ALIGN_LEFT, TabStop.LEAD_NONE);
}

TabSet tabset = new TabSet(tabs);
StyleContext sc = StyleContext.getDefaultStyleContext();
AttributeSet aset = sc.addAttribute(SimpleAttributeSet.EMPTY, StyleConstants.TabSet, tabset);
textPane.setParagraphAttributes(aset, false);

Solution

  • From the link

    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");
            frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    
            JEditorPane edit=new JEditorPane();
            edit.setEditorKit(new TabSizeEditorKit());
            try {
                edit.getDocument().insertString(0,"1\t2\t3\t4\t5", new SimpleAttributeSet());
            } catch (BadLocationException e) {
                e.printStackTrace(); 
            }
            frame.getContentPane().add(new JScrollPane(edit));
    
            frame.setSize(300,100);
            frame.setLocationRelativeTo(null);
            frame.setVisible(true);
        }
    
    
        static class CustomTabParagraphView extends ParagraphView {
    
            public CustomTabParagraphView(Element elem) {
                super(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);
            }
    
        }
    }