Solved

Problem - characters overlap in JTextPane

Posted on 2003-11-12
9
370 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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
topping3 challenge 14 70
Need to track down Infection in a Server 2008 domain user profile 7 48
eclipse java  build path 6 44
Way to decrease size of apk file 9 43
By the end of 1980s, object oriented programming using languages like C++, Simula69 and ObjectPascal gained momentum. It looked like programmers finally found the perfect language. C++ successfully combined the object oriented principles of Simula w…
Introduction Java can be integrated with native programs using an interface called JNI(Java Native Interface). Native programs are programs which can directly run on the processor. JNI is simply a naming and calling convention so that the JVM (Java…
Viewers learn about the “for” loop and how it works in Java. By comparing it to the while loop learned before, viewers can make the transition easily. You will learn about the formatting of the for loop as we write a program that prints even numbers…
Viewers learn about the scanner class in this video and are introduced to receiving user input for their programs. Additionally, objects, conditional statements, and loops are used to help reinforce the concepts. Introduce Scanner class: Importing…

911 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

20 Experts available now in Live!

Get 1:1 Help Now