Problem - Text in JTextPane overlaps

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

Are you adding the text from the other user in another thread than the event thread? Swing is not thread safe and strange things might happen if you update components from other threads. The solution would be to only update from the event thread. Look at the method SwingUtilities.invokeLater() for a way to do that: http://java.sun.com/j2se/1.4.2/docs/api/javax/swing/SwingUtilities.html#invokeLater(java.lang.Runnable)

Similar Messages

  • How to listen and obtain caret positions of changed text in JTextPane?

    Hi,
    I have a JTextPane that displays text with some styles on particular words in the text. For example, I highlight words with red color if they are in my dictionary file. I use regular expression to find matches, replace them with their corresponded definitions, and call setCharacterAttributes to add styles to the definitions. I store all start and end caret positions of the matched words/definitions in a vector. So, I can redisplay styles of all previous matches.
    The problem is that I'd like to be able to edit the text of some previous matches. So, with those changes all caret positions that I already store in the vector need to be updated.
    How can I obtain caret positions of changed text in JTextPane?
    How can I know that a user is currently changing text in the JTextPane?
    How can I get style of text such as color that is being changed?
    Thank you very much.

    Thank you very much, camickr, for your reply.
    I think that I might not know the right way to handle JTextPane and Document object.
    What I have done are:
    - Add style to JTextPane using, for example,
    Style targetCurrentMatchStyle = this.targetWindowTextPane.addStyle("Red", null);
    StyleConstants.setForeground(targetCurrentMatchStyle, Color.red);//For highlight - Then, I use regular expression (Pattern and Matcher) to find a match in text. For each match, I get start and end position from matcher.start() and matcher.end().
    if(matcher.find(start)){
    String term=matcher.group();
    int start=matcher.start();
    int end = matcher.end();
    //find definition for the matched term.
    String definition=mydictionaryHash.get(term);
    //Store caret positions in lists
    startPositionList.add(start);
    matchedLength=lengthList.add(definition.length());
    //Add changed to text in textpane
    StringBuffer sb=new StringBuffer();
    matcher.appendReplacement(sb, definition);
    matcher.appendTail(sb);
    //Get translated text from StringBuffer after replacement
    String translatedText=sb.toString();
    targetWindoTextPane.setText(translatedText);
    //Update start position for next search
    start=start+definition.length();
    //Add style to matched regions below.
    }- From the lists of start positions and matched lengths, I use the following code to add "Red" color to the matched text regions including all previously matched.
    for(int i=0;i<startPositionList.size();i++){
    this.targetWindowTextPane.getStyledDocument().setCharacterAttributes(
    startPositionList.get(i),
    lengthList.get(i),
    this.targetWindowTextPane.getStyle("Red"),
    true);
    }My issue is that I'd like to be able edit previously matched regions and update all positions of the matched regions stored in the lists.

  • How to get the bold texts in JTextPane?

    How to get the bold texts in JTextPane?

    I believe you need to look at the Elements of the Document. Use the Document.getDefaultRootElement() method to get the first root Element. From there you can iterate through all the Elements. You should then be able to check the attributes of the element to see if it contains the Bold attribute using something like:
    AttributeSet attributes = element.getAttributes();
    if ( attributes.containsAttribute(StyleConstants.Bold, Boolean.TRUE)
    // do processing here

  • How to get the current location text in JTextPane

    hi
    i want to get the current location text in JTextPane ...iam useing
    caretPos = jtpMain.getCaretPosition();
    iam geting the current Locations at the same time i want to read the entery current line
    i used like this
    StartPos=jtpMain.getText();
    but iam geting entry data in JTextPane ... how can get current Locatin data ...
    pls help me

    If I understood your request, what you would like to do is to be able to retrieve Start and End Positions of a given line (a line and NOT the whole content of the JTextPane).
    JTextPane is a graphical component that is bound to a logical document. Documents are structured in elements. What defines an element is up to you to decide if you extend your own document but elements are by default paragraphs (runs of text ended by a carriage return '\n' character). So, I think that what you want to do is to retrieve the Start and End positions of the
    paragraph over which the caret is currently positionned. Then I think you have got to do the following :
    int intStartPos;
    int intEndPos;
    int intCaretPos;
    javax.swing.text.StyledDocument docStyledDoc;
    javax.wing.text.Element eleParagraph;
    intCaretPos = jtpMain.getCaretPosition( );
    //Get a reference to the default styled document bound to the JTextPane component
    docStyledDoc = jtpMain.getStyledDocument( );
    //Get the paragraph element from the document over which the caret is positionned
    eleParagraph = docStyledDoc.getParagraphElement(intCaretPos);
    //Get paragraph's offsets
    intStartPos = eleParagraph.getStartOffset( );
    intEndPos = eleParagraph.getEndOffset( );
    hi
    i want to get the current location text in JTextPane
    ...iam useing
    caretPos = jtpMain.getCaretPosition();
    iam geting the current Locations at the same time i
    want to read the entery current line
    i used like this
    StartPos=jtpMain.getText();
    but iam geting entry data in JTextPane ... how can
    get current Locatin data ...
    pls help me

  • Why is text garbled and overlapped when I paste in Flash

    Why is text garbled and overlapped when I paste in Flash from other sources. The documentations suggests you can paste and text will fill columns automatically but I just get a clump of text in a very small space.

    Thanks a lot. It was the leading. It wasn't negative but it needed to be a lot higher. I really appreciate the help!!

  • How to append text to jtextpane

    i want to append the text to jtextpane,but i can't find the append() method like in jtextarea , is there any way to append the text to jtextpane

    Actually that should be:
    textPane.getDocument().insertString(...);

  • Is anybody having a problem texting photos to non iPhone users?

    Is anybody having problems texting photos to non iPhone users?

    Mine does it to iPhones not using iOS 6 as well. It also sometimes says that text messages haven't been sent though sometimes they have been. So people get them in duplicate

  • How can i Change the Size of the selected text in JTextPane using ConboBox

    plzz help...
    How can i Change the Size of the selected text in JTextPane using ConboBox ???
    i m using if(cb.getSelectedItem=="small")
    cb.setAction(new StyledEditorKit.FontSizeAction("double click", 12);)
    if(cb.getSelectedItem=="medium")
    cb.setAction(new StyledEditorKit.FontSizeAction("double click", 14);)
    if(cb.getSelectedItem=="large")
    cb.setAction(new StyledEditorKit.FontSizeAction("double click", 16);)
    this code is not working properly according to the action i set on comboBox.
    when i select medium the previously set action on comboBox works like small action work.
    when i select large the medium action starts .
    means its not working in correct time when i select item of combox n action of that item is not working at that time..
    plzz plzz help me:(

    Action action1 = new StyledEditorKit.FontSizeAction(
    "double click", 12);
    Action action2 = new StyledEditorKit.FontSizeAction(
    "double click", 14);
    Action action3 = new StyledEditorKit.FontSizeAction(
    "double click", 18);
    s2 = (String) cb7.getSelectedItem();
    if (s2.equals("Small")) {
    cb7.setAction(action1);
    e1.setSource(cb7);
    } else
    if (s2.equals("Medium")) {
    cb7.setAction(action2);
    e1.setSource(cb7);
    } else if (s2.equals("Large")) {
    cb7.setAction(action3);
    // e1.setSource(cb7);
    when i chooze any combobox item then according to that item i set the Action on ComboBox but that action is not working properly on the selected text in the JTextPane..means selected text in JText Pane is not changes its Size according to the comboBox selected ITEM.
    PLZ plzzzzzzzzzz help me:((.i will be thankfull to u.

  • The footer(Static Text) is getting overlapped on graph in OBIEE11g

    Hi all,
    Im using OBIEE11.1.1.6.0. when export a report into excel footer(static text) is getting overlapped on the graph.
    Some reports was exporting fine..bust most reports do not.
    How can fix this error..Pls help me.
    Rehargs
    944656
    Edited by: 944656 on Feb 6, 2013 10:52 PM
    Edited by: 944656 on Feb 18, 2013 5:18 AM

    Can u share the screen shot of this?

  • Info Popup : Element %_OUT2_%_APP_%-TEXT touches or overlaps

    Hi,
    There is a selection screen in our report for our tool.I get the information popup
    I get this popup when I click on the screen number in se80 to view the flow logic.
    Element %_OUT2_%_APP_%-TEXT touches or overlaps
    other element
    How to rectify this.
    Thanks

    Slection Screen
    SELECTION-SCREEN BEGIN OF SCREEN 200 AS SUBSCREEN.
    SELECTION-SCREEN BEGIN OF BLOCK OUTPUT WITH FRAME title text-006.
    PARAMETERS: OUT2 LIKE DETAILS-OUTPUT2 RADIOBUTTON GROUP OUT USER-COMMAND EXPORT DEFAULT 'X',
                OUT1 LIKE  DETAILS-OUTPUT1 RADIOBUTTON GROUP OUT.
    SELECTION-SCREEN END OF BLOCK OUTPUT.
    SELECTION-SCREEN BEGIN OF BLOCK EXPORT WITH FRAME title text-007.
    PARAMETERS: EXP1 LIKE  DETAILS-EXP_VER_YES RADIOBUTTON GROUP EXP MODIF ID EXP DEFAULT 'X',
                EXP2 LIKE  DETAILS-EXP_VER_NO RADIOBUTTON GROUP EXP MODIF ID EXP,
                PROTO LIKE DETAILS-PROT_EXP AS CHECKBOX MODIF ID EXP,
                DEL_LOG LIKE DETAILS-DEL_OLD_LOG AS CHECKBOX MODIF ID EXP,
                ACT_DDIC LIKE DETAILS-EXP_INA_DDI_OBJ AS CHECKBOX MODIF ID EXP.
    SELECTION-SCREEN END OF BLOCK EXPORT.
    SELECTION-SCREEN END OF SCREEN 200.
    Flow logic
    PROCESS BEFORE OUTPUT.
    MODULE %_INIT_PBO_J.
    MODULE %_PBO_REPORT.
    MODULE %_PF_STATUS.
    MODULE %_END_OF_PBO.
    PROCESS AFTER INPUT.
      MODULE %_INIT_PAI_J.
    CHAIN.
      FIELD OUT2    .
      FIELD OUT1    .
        MODULE %_RADIOBUTTON_GROUP_OUT                           .
    ENDCHAIN.
    CHAIN.
      FIELD OUT2    .
      FIELD OUT1    .
        MODULE %_BLOCK_0200001.
    ENDCHAIN.
    CHAIN.
      FIELD EXP1    .
      FIELD EXP2    .
        MODULE %_RADIOBUTTON_GROUP_EXP                           .
    ENDCHAIN.
    FIELD !PROTO MODULE %_PROTO .
    FIELD !DEL_LOG MODULE %_DEL_LOG .
    FIELD !ACT_DDIC MODULE %_ACT_DDIC .
    CHAIN.
      FIELD EXP1    .
      FIELD EXP2    .
      FIELD PROTO .
      FIELD DEL_LOG .
      FIELD ACT_DDIC .
        MODULE %_BLOCK_0200005.
    ENDCHAIN.
    CHAIN.
      FIELD OUT2    .
      FIELD OUT1    .
      FIELD EXP1    .
      FIELD EXP2    .
      FIELD PROTO .
      FIELD DEL_LOG .
      FIELD ACT_DDIC .
      MODULE %_END_OF_SCREEN.
      MODULE %_OK_CODE_1000.
    ENDCHAIN.

  • Why can't i send or receive text messages to some users from my iPad while there is no problem texting them from my iPhone?

    Why can't I send or receive text messages to some users from my iPad mini while there is no problem texting them from my iPhone?

    With the Apple iMessage app, You can only Message with other iOS users - iPads, iPod Touch, iPhone users and Macs running Mountain Lion or higher. With your iPhone, you can send standard SMS text messages to any phone, including Android phones.
    The iPad cannot send SMS messages or receive them either.

  • ComboBox Problem to change the size of selected text in JTextPane.

    hi 2 all!
    I have a problem in combo box actions, when i change the item in the combo its set is not set at that time but that action is performed when next action is taken, i have used combobox.setAction(new StyledEditorKit.FontSizeAction(" click", 12);) command to set the size of the selected text
    Plz suggest the solution also if possible,plz provide some code for this
    Action action1 = new StyledEditorKit.FontSizeAction(
                                                 "double click", 12);
                                       Action action2 = new StyledEditorKit.FontSizeAction(
                                                 "double click", 14);
                                       Action action3 = new StyledEditorKit.FontSizeAction(
                                                 "double click", 18);
    s2 = (String) cb7.getSelectedItem();
                                       if (s2.equals("Small")) {
                                            cb7.setAction(action1);
                                            e1.setSource(cb7);
                                                      } else
                                       if (s2.equals("Medium")) {
                                            cb7.setAction(action2);
                                            e1.setSource(cb7);
                                                                          } else if (s2.equals("Large")) {
                                            cb7.setAction(action3);
                                            // e1.setSource(cb7);
    when i chooze any combobox item then according to that item i set the Action on ComboBox but that action is not working properly on the selected text in the JTextPane..means selected text in JText Pane is not changes its Size according to the comboBox selected ITEM.
    PLZ plzzzzzzzzzz help me:((.i will be thankfull to u.
    thanx in advance..

    this code is not working properly according to the action i set on comboBox.Thats correct, the setAction() method is used to invoke an existing Action on the combo box, not create a new Action.
    What you need to do is have a single action that uses the information from the item that was selected to build a dynamic Action to change the font. Something like:
    public void actionPerformed(ActionEvent e)
         JComboBox comboBox = (JComboBox)e.getSource();
         int fontSize = Integer.parseInt( comboBox.getSelectedItem().toString() );
         Action fontAction = new StyledEditorKit.FontSizeAction("size", fontSize);
         fontAction.actionPerformed(null);
    }

  • Problems with printing the text in JTextPane

    Hello
    I need to print the text in a text pane. It prints the first page of the text pane and it doesn't print the rest of the text. I don't know what the problem is.
    The code I am using, is below. I
    Thank you.
    LilyLay
    import java.awt.*;
    import javax.swing.*;
    import java.awt.print.*;
    public class PrintUtilities implements Printable {
    private Component componentToBePrinted;
    public static void printComponent(Component c) {
    new PrintUtilities(c).print();
    public PrintUtilities(Component componentToBePrinted) {
    this.componentToBePrinted = componentToBePrinted;
    public void print() {
    PrinterJob printJob = PrinterJob.getPrinterJob();
    printJob.setPrintable(this);
    if (printJob.printDialog())
    try {
    printJob.print();
    } catch(PrinterException pe) {
    System.out.println("Error printing: " + pe);
    public int print(Graphics g, PageFormat pageFormat, int pageIndex) {
    if (pageIndex > 0) {
    return(NO_SUCH_PAGE);
    } else {
    Graphics2D g2d = (Graphics2D)g;
    g2d.translate(pageFormat.getImageableX(), pageFormat.getImageableY());
    disableDoubleBuffering(componentToBePrinted);
    componentToBePrinted.paint(g2d);
    enableDoubleBuffering(componentToBePrinted);
    return(PAGE_EXISTS);
    public static void disableDoubleBuffering(Component c) {
    RepaintManager currentManager = RepaintManager.currentManager(c);
    currentManager.setDoubleBufferingEnabled(false);
    public static void enableDoubleBuffering(Component c) {
    RepaintManager currentManager = RepaintManager.currentManager(c);
    currentManager.setDoubleBufferingEnabled(true);

    public int print(Graphics g, PageFormat pageFormat, int pageIndex) {
    if (pageIndex > 0) {
    return(NO_SUCH_PAGE);
    } else {if (pageIndex > 0) return (NO_SUCH_PAGE)
    prevents printing any more than the first page.
    change >0 to >= numPages
    Howard Amos

  • Problem about using JTextPane in JScrollPane

    I put a JTextPane in a JScrollPane.
    But when I reduce the window size of the JScrollPane with my mouse, the text line in the JTextPane is broken to newlines automatically due to the reduced window size.
    How to prevent the text lines breaking in this situation!

    OK, so the words are wrapping when they're not suppose to. How are you adding your JTextPane to the JScrollpane? Also, does setting a preferred size or a minimum size on the JTextPane help?
    That's an odd problem. In looking at the API I see that JTextArea and JTextPane are both derived (eventually) from JTextComponent. But the line wrap option is only in JTextArea. This makes me think that it would be possible to overload paint method of the JTextPane. But wow! That would be messy.
    I'll keep thinking about it. Let us know if you find a solution.

  • Text field contents overlap the static text below.

    hi all,
    I have got a problem with the size of a text field in my form. In the form, below the text field, i have got a static text. The height of the text field varies dynamically depending on the size of the string coming from the backend.
    Now, as the size of the text field increases, its text is overlapping the static text below. i dont wat this to happen. how can i dynamically change the position of the static text depending on the size of the text field above.
    regards
    Shyam.

    hi,
    with regard to your problem please go through the following link where I have provided the solution similar to your problem.
    Re: Display more lines of table than size of current page
    Try out this and let me know if you still have problems.
    Thanks,
    kris

Maybe you are looking for