Solved

Problem - characters overlap in JTextPane

Posted on 2003-11-12
9
366 Views
Last Modified: 2006-11-17
Hi All,

   I have an application that allows 2 users (User A + User B) to chat
interactively. Every character User A / User B inputs will be sent to the other
side immediately. Only one of the users is "active" - only characters input by the
"active" user will be sent to the other side. When the "active" user has finished
typing, he/she will press a button to indicate he/she is done and the other side
will become "active".

   Characters input by User A / User B will be displayed in an instance of
JTextPane at the other side. An instance of DefaultStyledDocument is used as the
only argument of the constructor that is called to initialize the instance of
JTextPane. JTextPane is used because different colors has to be used to identify
characters input by the 2 users.

   Below is some lines of code of the application.

      StyledDocument doc = new DefaultStyledDocument();
      JTextPane txtReceiveBuffer = new JTextPane(doc);
      SimpleAttributeSet attribute = new SimpleAttributeSet();
      ...
      StyleConstants.setForeground(attribute, Color.blue);
      ...
      doc.insertString(doc.getLength(), strUserInput, attribute);
      ...

   Sometimes a line of characters in the JTextPane will overlap the other line.
(It seems like a transparent layer with some characters is placed in front of
another layer with some characters.) Sometimes the line "behind" will totally
disappear. (It seems like a non-transparent layer with some characters is placed
in front of another layer with some characters.) If a user tries to resize the
frame that contains the JTextPane, sometimes things will be fine again, but
sometimes the problem doesn't go until the application is restarted.

   Has anyone ever encountered the same problem? And does anyone get a
solution to this problem?

   Thanks in advance.

Paul
0
Comment
Question by:paulyu
  • 4
  • 3
9 Comments
 
LVL 15

Expert Comment

by:jimmack
ID: 9730962
How are you adding the text to the JTextPane?

Does this problem only occur when a new line should have been inserted (eg. it looks like it's done a carriage return, but not a linefeed), or do the characters overwrite each other as they are added?
0
 
LVL 15

Expert Comment

by:jimmack
ID: 9730967
Oops.  Sorry.  Ignore the first question ;-)  I see the insertString now :-)
0
 
LVL 2

Expert Comment

by:Dannin
ID: 9732401
can u append instead of inserting?
0
How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

 
LVL 2

Accepted Solution

by:
Dannin earned 63 total points
ID: 9732456
Have you tried using a regular document class? or it wont meet ur needs

doc.insertString(doc.endPosition(), strUserInput, attribute);

try that perhaps=)
0
 
LVL 15

Assisted Solution

by:jimmack
jimmack earned 62 total points
ID: 9732483
Dannin:  The insertString is the normal way of doing this.

paulyu:  There is no need to use a StyledDocument if you are only interested in the use of color.  This can be done with the default document (javax.swing.text.Document).
0
 
LVL 15

Expert Comment

by:jimmack
ID: 9732503
;-)  Cross posting there I think :-)
0
 
LVL 2

Expert Comment

by:Dannin
ID: 9732513
ya I notice it was the normal way after reading the api =P that's why I suggested a normal document class=P
0

Featured Post

Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

Join & Write a Comment

Java contains several comparison operators (e.g., <, <=, >, >=, ==, !=) that allow you to compare primitive values. However, these operators cannot be used to compare the contents of objects. Interface Comparable is used to allow objects of a cl…
Java Flight Recorder and Java Mission Control together create a complete tool chain to continuously collect low level and detailed runtime information enabling after-the-fact incident analysis. Java Flight Recorder is a profiling and event collectio…
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:
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …

757 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

Need Help in Real-Time?

Connect with top rated Experts

17 Experts available now in Live!

Get 1:1 Help Now