Question

While having quite some free time to use, I ran into a problem concerning a StyledDocument as part of a JTextPane. I want to remove the first part of the text when the total size of the Document reaches value x (10.000 in my case). But since it is important that lay-out is being kept, let me show you what happens in an image first, after that I'll post the source.

In image 1: When the maximum length isn't yet reached, I have no Horizontal Scrollbar

In image 2: When the maximum length of 10.000 characters is reached, and the StyledDocument is cropped with styleddoc.getDocument().remove(0, maxsize);

http://i.stack.imgur.com/i10KZ.png [Image 1]

http://i.stack.imgur.com/dUZ0K.png [Image 2]

(I am very sorry, but since this is my first question here, I am not allowed to post pictures as a spam rejection measure)

As you see, the horizontar scrollbarr suddenly appears, my lay-out is completely messed-up, and under no circumstance, will the text be neatly divided into several lines when it doesn't fit, like it did before the remove.

Now, here's my source. Please help me find a genius solution to this problem.

public void publicTextPaneOutput(String sender, String receiver, int type, String message) {
    int messagesize = 0;
    StyledDocument styledDocument = (StyledDocument) publicText.getDocument();
    Style defaultStyle = styledDocument.addStyle("Default Text", null);
    Style nicknameStyle = styledDocument.addStyle("Personal Nickname", null);
    StyleConstants.setBold(nicknameStyle, true);
    StyleConstants.setForeground(nicknameStyle, Color.decode("#006400"));
    Style chatterStyle = styledDocument.addStyle("Other Nickname", null);
    StyleConstants.setBold(chatterStyle, true);
    StyleConstants.setForeground(chatterStyle, Color.blue);

    switch (type) {
        case 1: {
            messagesize += sender.length() + message.length() + 3;
            publicTextPaneOverflowProtection(styledDocument, messagesize);
            try {
                if (sender.equals(nickname)) {
                    styledDocument.insertString(styledDocument.getLength(), (new StringBuilder()).append(sender).toString(), nicknameStyle);
                } else {
                    styledDocument.insertString(styledDocument.getLength(), (new StringBuilder()).append(sender).toString(), chatterStyle);
                }
                styledDocument.insertString(styledDocument.getLength(), ": ", chatterStyle);
                styledDocument.insertString(styledDocument.getLength(), message, defaultStyle);
                styledDocument.insertString(styledDocument.getLength(), "\n", defaultStyle);
            } catch (BadLocationException e) {
                e.printStackTrace();
            }
            break;
        }
        case 2: {
            messagesize += sender.length() + receiver.length() + message.length() + 7;
            publicTextPaneOverflowProtection(styledDocument, messagesize);
            try {
                if (sender.equals(nickname)) {
                    styledDocument.insertString(styledDocument.getLength(), (new StringBuilder()).append(sender).toString(), nicknameStyle);
                } else {
                    styledDocument.insertString(styledDocument.getLength(), (new StringBuilder()).append(sender).toString(), chatterStyle);
                }
                styledDocument.insertString(styledDocument.getLength(), (new StringBuilder()).append(" -> ").toString(), chatterStyle);
                if (receiver.equals(nickname)) {
                    styledDocument.insertString(styledDocument.getLength(), (new StringBuilder()).append(receiver).toString(), nicknameStyle);
                } else {
                    styledDocument.insertString(styledDocument.getLength(), (new StringBuilder()).append(receiver).toString(), chatterStyle);
                }
                styledDocument.insertString(styledDocument.getLength(), (new StringBuilder()).append(": ").toString(), chatterStyle);
                styledDocument.insertString(styledDocument.getLength(), message, defaultStyle);
                styledDocument.insertString(styledDocument.getLength(), "\n", defaultStyle);

            } catch (BadLocationException e) {
                e.printStackTrace();
            }
            break;
        }
    }
    scroll();
}

private void publicTextPaneOverflowProtection (StyledDocument styledDocument, int messagesize)
{
    if(styledDocument.getLength() + messagesize > 10000) {
        try {
            styledDocument.remove(0, messagesize);
        } catch (BadLocationException e) {

        }
    }

}

public void scroll() {
    StyledDocument styleddocument = (StyledDocument) publicText.getDocument();
    publicText.setCaretPosition(styleddocument.getLength());
}

// this part is coded in the class itself

private JTextPane publicText;
private JScrollPane publicTextScrollPane;

//this is coded in the constructor of the class
publicText = new JTextPane();
publicTextScrollPane = new JScrollPane();

publicText.setEditable(false);
publicTextScrollPane.setViewportView(publicText);

In case anyone needs additional code of a certain part of my project, please tell me a.s.a.p.

The solution I want to have should implement this removing of text at the beginning of the StyledDocument, and hold the placed \n characters in account, even is a part is removed. I have tried several things, even to use just a plain "Document" in stead of a Styled one, but this is no option, since I plan to implement usage of colors and emoticons in my chatapplet.

Every single one is thanked on beforehand for looking into this! I'll follow up as much as possible.

With kind regards, Steven Kusters

PS.: Why am I removing parts of a StyledDocument? Memory management, because this is part of a chat Applet which can be used by up to 200 people at the same time, and when everyone says something every 10 seconds, after one minute you have 1200 messages, which will use a lot of memory when this continues for lets say, 10 hours: 720.000 messages as an estimated max load. (worst-case scenario (or best-case, depends on the point of view))

Was it helpful?

Solution

Your question has a lot of different parts, but one part may be pretty straightforward to fix. It sounds like your primary concern is how to remove lines from your document. Someone has implemented a DocumentListener that handles this aspect for you. Check out LimitLinesDocumentListener. That page links to the source for the class, which also has examples on how to remove lines from your document.

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