The answer is below. It is based on setting a document filter on the component's document object. The (static) install() method can do all the setup for you.
package com.blogspot.javadots;
import javax.swing.text.AbstractDocument;
import javax.swing.text.AttributeSet;
import javax.swing.text.BadLocationException;
import javax.swing.text.DocumentFilter;
import javax.swing.text.JTextComponent;
public class TabToSpaceFilter extends DocumentFilter {
private String spaces = "";
private JTextComponent textComponent;
public TabToSpaceFilter(int tabSize, JTextComponent tc) {
textComponent = tc;
for(int i = 0; i < tabSize; ++i)
spaces += " ";
}
@Override
public void insertString(FilterBypass fb, int offset, String text,
AttributeSet attr) throws BadLocationException {
super.insertString(fb, offset, translate(offset, text), attr);
}
@Override
public void replace(FilterBypass fb, int offset, int length,
String text, AttributeSet attr) throws BadLocationException {
super.replace(fb, offset, length, translate(offset, text), attr);
}
private String translate(int offset, String s) {
int col = columnOf(offset);
StringBuilder sb = new StringBuilder();
int top = s.length();
for(int i = 0; i < top; ++i, ++col) {
char c = s.charAt(i);
if(c == '\t')
sb.append(spaces.substring(col % spaces.length()));
else
sb.append(c);
}
return sb.toString();
}
private int columnOf(int i) {
String s = textComponent.getText();
if(i == 0)
return 0;
int prev = s.lastIndexOf("\n", i-1);
if(prev < 0)
return i;
return (i-prev)-1;
}
public static void install(int tabSize, JTextComponent area) {
AbstractDocument ad = (AbstractDocument) area.getDocument();
ad.setDocumentFilter(new TabToSpaceFilter(tabSize, area));
}
}
Nice Blog Usefull information Java Development
Unknown
May 22, 2015 at 12:39 PM"I am really impressed with your writing skills and also with the layout on your blog. "
ecorp trainings
Unknown
December 8, 2015 at 4:09 PMit is good blog and explanation is too good.very valuable subject you have given.oracle fusion SCM on line training
ORACLE FUSION PROCUREMENT
March 13, 2017 at 7:57 AMvery informative article.And very well explained about different protocols.keep posting like good content posts.Further details please visit our website..
Oracle Fusion Training Institute
Unknown
March 14, 2017 at 11:22 AMHi,
It's a very nice article,Surely this will help to people of Jaava Community. I Appreciate for your work,Keep move on with new articles providing useful information.
Thank you.
oracle EBS training
oracleR12
March 22, 2017 at 1:05 PMThanks for posting the blog. This was so interesting blog, I felt comfortable while reading the post, thank you…………………….. For More details about Oracle Fusion Financials Training please click here.
Sandeep SEO
December 20, 2017 at 6:33 AMThis blog explains the details of most popular technological details. This helps to learn about what are all the different method is there. And the working methods all of that are explained here. Informative blog.
Oracle Fusion HCM Training in Hyderabad
Unknown
August 16, 2018 at 12:50 PMNice Info, thanks for sharing with us
Java training in Hyderabad
TRONIX
February 22, 2023 at 5:38 AMThank you for sharing this Beautiful Blog.....
Azure DevOps Training in Hyderabad
Digital Learning
September 19, 2023 at 10:20 AMvery nice blogger thanks for sharing
Testing tools Training in Hyderabad
Digital Learning
November 15, 2023 at 8:06 AM"I am really impressed with your writing skills, Nice blog
Keep posting
vv software
December 22, 2023 at 8:04 AM"Valuable one...thanks for sharing..
SQL SERVER training in Hyderabad
Digital Learning
June 13, 2024 at 1:25 PM