Terai Atsuhiro 2023-04-10 (月) 00:10:17

Java Forums - tabsize in JTextPane

http://terai.xrea.jp/swing/tabsize/screenshot.png

textpane.setFont(new Font("monospaced", Font.PLAIN, 12));
FontMetrics fm = textpane.getFontMetrics(textpane.getFont());
int charWidth = fm.charWidth('m');
int tabWidth = charWidth * 4;
TabStop[] tabs = new TabStop[10];
for(int j=0;j<tabs.length;j++) {
  tabs[j] = new TabStop((j+1)*tabWidth);
}
TabSet tabSet = new TabSet(tabs);
SimpleAttributeSet attrs = new SimpleAttributeSet();
StyleConstants.setTabSet(attrs, tabSet);
int l = textpane.getDocument().getLength();
textpane.getStyledDocument().setParagraphAttributes(0, l, attrs, false);