views:

31

answers:

1

I'm wondering if there's a way to have tabs convert to spaces in a jeditorpane, much like you'd see when working in an IDE. I don't want to set the tab size. I can already do that easily.

I want to have tabs replaced with their equivalent in spaces. So for example if my tabs are 5 spaces long, I would want all tabs replaced immediately with 5 spaces whenever they are created.

Any ideas?

+2  A: 

Add a DocumentFilter to the Document to replaces tabs with spaces as text is inserted into the Document. Read the section from the Swing tutorial on Text Component Features for more information.

camickr
You can also set your own Document, to transform all tabs to spaces. See http://java.sun.com/javase/6/docs/api/javax/swing/text/JTextComponent.html#setDocument%28javax.swing.text.Document%29
Istao
Perfect - thanks!
Cam
Yes you can use a custom Document but the preferred approach is to use the DocumentFilter. A filter can be used on any AbstractDocument so the code is more reusable and can therefore be used on a JTextArea or a JTextPane.
camickr
Yeah I ended up using a DocumentFilter - that way like camickr said once I have it all set up, I can easily replicate the behaviour later for another text field so long as it extends AbstractDocument.
Cam