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
Solved

Problem - characters overlap in JTextPane

Posted on 2003-11-12
9
375 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
Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

 
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

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Java had always been an easily readable and understandable language.  Some relatively recent changes in the language seem to be changing this pretty fast, and anyone that had not seen any Java code for the last 5 years will possibly have issues unde…
Introduction This article is the second of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers the basic installation and configuration of the test automation tools used by…
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 will learn about the regular for loop in Java and how to use it. Definition: Break the for loop down into 3 parts: Syntax when using for loops: Example using a for loop:

856 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