Solved

Problem - characters overlap in JTextPane

Posted on 2003-11-12
9
385 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Creating Instructional Tutorials  

For Any Use & On Any Platform

Contextual Guidance at the moment of need helps your employees/users adopt software o& achieve even the most complex tasks instantly. Boost knowledge retention, software adoption & employee engagement with easy solution.

 
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

Transaction Monitoring Vs. Real User Monitoring

Synthetic Transaction Monitoring Vs. Real User Monitoring: When To Use Each Approach? In this article, we will discuss two major monitoring approaches: Synthetic Transaction and Real User Monitoring.

Question has a verified solution.

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

Introduction This article is the first of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article explains our test automation goals. Then rationale is given for the tools we use to a…
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 …
Video by: Michael
Viewers learn about how to reduce the potential repetitiveness of coding in main by developing methods to perform specific tasks for their program. Additionally, objects are introduced for the purpose of learning how to call methods in Java. Define …
This theoretical tutorial explains exceptions, reasons for exceptions, different categories of exception and exception hierarchy.

728 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