Search code examples
javaswingjtextpane

Keeping the correct style on text retrieval


I am making an application similar to a chat. For that purpose I have two JTextPanes, one where I'm writing and one that displays messages. This is the code that handles the transferring of text from input to display :

                String input = textPane.getText();
                if(!input.endsWith("\n")){
                    input+="\n";
                }
                StyledDocument doc = displayPane.getStyledDocument();
                int offset = displayPane.getCaretPosition();
                textPane.setText("");
                try {
                    doc.insertString(offset, input, set);
                } catch (BadLocationException ex) {
                    Logger.getLogger(ChatComponent.class.getName()).log(Level.SEVERE, null, ex);
                }

The problem is that if i have colour on some words of the input text , the output is all coloured . So the colour is applied to all of the text when moved to display(while displayed correctly on input). Any suggestions on how i can move text properly?

Notice that same happens with other format as bold , italic etc


Solution

  • This has been solved by merging the document models of the two panes. The solution is in this question: Keeping the format on text retrieval