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

7 comments :: Swing - Tabs to Spaces

  1. Ecorptrainings.com provides JAVA SWING in hyderabad with best faculties on real time projects. We give the best online trainingamong the JAVA SWING in Hyderabad. Classroom Training in Hyderabad India

  2. Nice Blog Usefull information Java Development

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

  4. I have read your blog its very attractive and impressive. I like it your blog.

    Java Training in Chennai Java Training in Chennai | Core Java Training in Chennai

    Online Java Training Online Java Training | Java J2EE Online Training | JavaEE Training Institute in Chennai m

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

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

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

Post a Comment