[Last Call] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 403
  • Last Modified:

Problem - characters overlap in JTextPane

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
paulyu
Asked:
paulyu
  • 4
  • 3
2 Solutions
 
jimmackCommented:
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
 
jimmackCommented:
Oops.  Sorry.  Ignore the first question ;-)  I see the insertString now :-)
0
 
DanninCommented:
can u append instead of inserting?
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.

 
DanninCommented:
Have you tried using a regular document class? or it wont meet ur needs

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

try that perhaps=)
0
 
jimmackCommented:
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
 
jimmackCommented:
;-)  Cross posting there I think :-)
0
 
DanninCommented:
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

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

  • 4
  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now