Question

I have a thread which functions as follows: characters are continuously entered, which are being stored in a StringBuilder. The contents of this string builder need to be styled according to some codes. As the character is appended in the string builder, I have a loop that splits this into an array of strings, and does some processing. Here is small (but complete in itself processing):

SimpleAttributeSet set = new SimpleAttributeSet();

if(string.contains(code1)){ 
    str = string;               
    int index = string.indexOf(code1); 
    string = string.replaceAll(code1, "");
    StyleConstants.setForeground(set, Color.GREEN);

    // data is a another variable of string builder
    data.append(string);
    i = sb.indexOf(string);

    String st = string.substring(index); 

    doc.setCharacterAttributes(data.indexOf(st), st.length(), set, true);
}

//similarly there are checks and styling for other codes

//terminalArea is of JTextPane type
terminalArea.setText(dataToShow.toString());

The text is colored correctly according to the codes, But as the data is displayed, the colors appear for an instant, and then lost. After that that, when a key is pressed, the colors appear again, but as the key is released, they lost.

I tried terminalArea.repaint(), but nothing happens.

Update: With respect to StanislavL's answer, Here is what I am having now:

doc = new DefaultStyledDocument();
if(string.contains(code)){
        str = string;
        int index = string.indexOf(code); 
        string = string.replaceAll(code, "");
        StyleConstants.setForeground(set, Color.GREEN);
        dataToShow.append(string);
        i = sb.indexOf(string);
        String st = string.substring(index); 
        doc.setCharacterAttributes(dataToShow.indexOf(st), st.length(), set, true);
        doc.insertString(doc.getLength() , string, null);


        }
//after all conditions
terminalArea.setDocument(doc);

Though the color persists, but only that text is shown colored, which was colored last. The previous ones are shown in white.Please note, the whole string is not colored, only a part of it is colored.

Solved:

StanislavL's answer solved the problem. I overlooked this thing that I was setting the character attributes first, then inserting the array. Interchanging the position solved the issue.

Was it helpful?

Solution

I assume you use StyledEditorKit.

terminalArea.setText(dataToShow.toString());

The code just erases all you have and readd the text to the document. In fact in the document remove(0, docLength) is called and then insertString(theToStringResult) replaces all the styled.

I would recomment to create a new Document instance, insert all the texts with styled using insertString() where you can pass your attributes (or call insertString() just once and the call multiple times setCharacterAttributes()). After the Document instance is ready just call terminalArea.setDocument(theDocumentInstance); rather than setText();

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top