?
Solved

Replace key presses in a JTextPane

Posted on 2004-11-29
7
Medium Priority
?
333 Views
Last Modified: 2006-11-17
In a JTextPane, how would I insert four spaces instead of a tab when the tab key is pressed?
0
Comment
Question by:Breadstick
  • 4
  • 3
7 Comments
 
LVL 92

Expert Comment

by:objects
ID: 12701034
use a custom Document that would check for tabs being inserted and replace with spces.
0
 
LVL 2

Author Comment

by:Breadstick
ID: 12701044
I'm not quite sure how to do that... could you explain, or give an example?
0
 
LVL 92

Expert Comment

by:objects
ID: 12701094
something like:

public class MyDocument extends PlainDocument
{
    public void insertString(int offs, String str, AttributeSet a)
    {
        super.insertString(offs, str.replaceAll("\t", "    "), a);
    }
}
0
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
LVL 2

Author Comment

by:Breadstick
ID: 12701178
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
 
LVL 92

Accepted Solution

by:
objects earned 500 total points
ID: 12701194
try subclassing DefaultStyledDocument
0
 
LVL 2

Author Comment

by:Breadstick
ID: 12701258
That worked, thank you.
0
 
LVL 92

Expert Comment

by:objects
ID: 12701300
No worries :)
0

Featured Post

[Webinar] Cloud and Mobile-First Strategy

Maybe you’ve fully adopted the cloud since the beginning. Or maybe you started with on-prem resources but are pursuing a “cloud and mobile first” strategy. Getting to that end state has its challenges. Discover how to build out a 100% cloud and mobile IT strategy in this webinar.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This was posted to the Netbeans forum a Feb, 2010 and I also sent it to Verisign. Who didn't help much in my struggles to get my application signed. ------------------------- Start The idea here is to target your cell phones with the correct…
Introduction This article is the last of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers our test design approach and then goes through a simple test case example, how …
Viewers will learn one way to get user input in Java. Introduce the Scanner object: Declare the variable that stores the user input: An example prompting the user for input: Methods you need to invoke in order to properly get  user input:
This video teaches viewers about errors in exception handling.
Suggested Courses
Course of the Month15 days, 5 hours left to enroll

839 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question