3 Replies Latest reply on Aug 5, 2002 4:19 AM by 807577

    tabsize in JTextPane

    807577
      hi there

      how can one set the user defined tab size in JTextPane. i tried with the following code but to of no use.
      textpane.setDocument(new MyDocument());
       
      public class MyDocument extends DefaultStyledDocument {
       
          public MyDocument() {
              setTabSize(4);
          }
       
          public void setTabSize(int size) {
              size = size < 1 ? 1 : size;
              putProperty(PlainDocument.tabSizeAttribute, new Integer(size));
          }
       
          public int getTabSize() {
              Object o = getProperty(PlainDocument.tabSizeAttribute);
              return (o != null) ? ((Integer)o).intValue() : 8;
          }
       
      }
      can u ppl help me? thanx in advance.

      Afroze.
        • 1. Re: tabsize in JTextPane
          camickr
          Here is how you can set the tab size to be 4. Works best when using a monospaced font.
          import java.awt.*;
          import javax.swing.*;
          import javax.swing.text.*;
          
          public class TestTextPane extends JFrame
          {
               public TestTextPane()
               {
                    JPanel panel = new JPanel();
                    setContentPane( panel );
          
                    JTextPane textPane = new JTextPane();
                    textPane.setFont( new Font("monospaced", Font.PLAIN, 12) );
                    JScrollPane scrollPane = new JScrollPane( textPane );
                    scrollPane.setPreferredSize( new Dimension( 200, 200 ) );
                    panel.add( scrollPane );
                    setTabs( textPane, 4 );
               }
          
               public void setTabs( JTextPane textPane, int charactersPerTab)
               {
                    FontMetrics fm = textPane.getFontMetrics( textPane.getFont() );
                    int charWidth = fm.charWidth( 'w' );
                    int tabWidth = charWidth * charactersPerTab;
          
                    TabStop[] tabs = new TabStop[10];
          
                    for (int j = 0; j < tabs.length; j++)
                    {
                         int tab = j + 1;
                         tabs[j] = new TabStop( tab * tabWidth );
                    }
          
                    TabSet tabSet = new TabSet(tabs);
                    SimpleAttributeSet attributes = new SimpleAttributeSet();
                    StyleConstants.setTabSet(attributes, tabSet);
                    int length = textPane.getDocument().getLength();
                    textPane.getStyledDocument().setParagraphAttributes(0, length, attributes, true);
               }
          
               public static void main(String[] args)
               {
                    TestTextPane frame = new TestTextPane();
                    frame.setDefaultCloseOperation( EXIT_ON_CLOSE );
                    frame.pack();
                    frame.setVisible(true);
               }
          }
          • 2. Re: tabsize in JTextPane
            807577
            thanx camickr, its perfectly working.
            • 3. Re: tabsize in JTextPane
              807577
              TAB size setting will not work for "text/html" type ,do you have any workaround or tricks to to do the same ?