Replace key presses in a JTextPane

In a JTextPane, how would I insert four spaces instead of a tab when the tab key is pressed?
LVL 2
BreadstickAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

objectsCommented:
use a custom Document that would check for tabs being inserted and replace with spces.
0
BreadstickAuthor Commented:
I'm not quite sure how to do that... could you explain, or give an example?
0
objectsCommented:
something like:

public class MyDocument extends PlainDocument
{
    public void insertString(int offs, String str, AttributeSet a)
    {
        super.insertString(offs, str.replaceAll("\t", "    "), a);
    }
}
0
Cloud Class® Course: Microsoft Azure 2017

Azure has a changed a lot since it was originally introduce by adding new services and features. Do you know everything you need to about Azure? This course will teach you about the Azure App Service, monitoring and application insights, DevOps, and Team Services.

BreadstickAuthor Commented:
When I run that I get an illegal argument exception:

java.lang.IllegalArgumentException: Model must be StyledDocument
      at javax.swing.JTextPane.setDocument(JTextPane.java:112)
      at MyTextHolder.<init>(MyTextHolder.java:15)
      at RDE$2.run(RDE.java:830)
      at java.lang.Thread.run(Thread.java:534)

If I try to have MyDocument extend StyledDocument instead of PlainDocument, it says cannot resolve symbol where I call insertString.
0
objectsCommented:
try subclassing DefaultStyledDocument
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
BreadstickAuthor Commented:
That worked, thank you.
0
objectsCommented:
No worries :)
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Java

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.