Swing - Tabs to Spaces

How do you make a swing text component (JTextEditor, JEditorPane, etc.) to insert spaces whenever the user hits the "Tab" key?

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));
}
}

11 comments :: Swing - Tabs to Spaces

  1. Nice Blog Usefull information Java Development

  2. "I am really impressed with your writing skills and also with the layout on your blog. "
    ecorp trainings

  3. it is good blog and explanation is too good.very valuable subject you have given.oracle fusion SCM on line training

  4. very 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

  5. Hi,
    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

  6. Thanks 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.

  7. This 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

  8. Nice Info, thanks for sharing with us

    Java training in Hyderabad

  9. Thank you for sharing this Beautiful Blog.....

    Azure DevOps Training in Hyderabad

  10. very nice blogger thanks for sharing

    Testing tools Training in Hyderabad

  11. "I am really impressed with your writing skills, Nice blog

    Keep posting

Post a Comment