Here is a working example that sets each of the first 10 tabs to represent 4 characters (the default is 8). This is applied to the entire document. Hope this helps.
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) );
textPane.setText( "abcdefghijklmnop\n\tone\n\t\ttwo" );
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);
textPane.getStyledDocument().setParagraphAttributes(0, textPane.getDocument().getLength(), attributes, true);
}public static void main(String[] args)
{
TestTextPane frame = new TestTextPane();
frame.setDefaultCloseOperation( EXIT_ON_CLOSE );
frame.pack();
frame.setVisible(true);
}
}