user2596934
user2596934

Reputation: 17

Appending a String to a JTextPane

I have a question. I am appending a string in a JTextPane, chatWindow, with an insertString, but the only issue is that I don't know how to 'insertString' to my JTextPane. Here is my code:

private void showMessage(final String string){
            SwingUtilities.invokeLater(
                new Runnable(){
                    public void run(){
                        //chatWindow.append(string);
                        //THE BOTTOM METHOD IS USED FOR APPENDING A STRING JTEXTPANE STYLE
                        try
                        {
                            //doc.insertString(0, "Start of text\n", null );
                            //doc.insertString(doc.getLength(), "", string );
                            //doc.insertString(int offset, String str, ArributeSet a);

                            //SETTING THE STYLE FOR THE STRING (down below)

                            StyleConstants.setForeground(keyWord, Color.getHSBColor(251, 89, 87));
                            //StyleConstants.setBackground(keyWord, Color.YELLOW);
                            StyleConstants.setBold(keyWord, false);

                            doc.insertString(0, string, keyWord);
                        }
                        catch(Exception e) { System.out.println(e); }
                    }
                }
            );
        }

Where it says:

doc.insertString(0, string, keyword);

This is where I am appending my string to the chatWindow. My only issue is that I don't know how 'insertString' specifically to the chatWindow like how I did in the note above the try-catch method:

chatWindow.append(string)

Does anyone know I could use the 'doc.insertString(0, string, keyword);' to insert the string keyword to chatWindow? The result of doc.insertString doesn't show up on my chatWindow. Thanks.

Upvotes: 0

Views: 818

Answers (1)

StanislavL
StanislavL

Reputation: 57381

What about this?

chatWindow.getDocument().insertString(0, string, keyword); 

Upvotes: 1

Related Questions