I am making a small chat program and I want my display area (a JTextPane component) to handle smiles. That is, for example, if it's requested to display the string " Hello : ) ", it should recognize the smiley shortcut and render it as an icon instead of plain text.
I am thinking of overriding the insertString method of the Document class. I will then anylise the new entry looking for smilies shortcuts on it in order to apply the appropriate style for them.
what do you guys think? Is there anything more simple?
Stanislav, I read the sample you provided. Basically there's a listener registered on the Pane's document that looks for smilies shortcuts once a new entry has been fired. As soon as it finds one, it schedules a new task to remove that shortcut from the document and insert and icon instead. This works fine, but how's that better than what I thought of? I find overriding the insertString method of the DefaultStyledDocument class more simple, no?
Joined: Dec 01, 2009
In simplest EditorKits insertString() is enough but you will have one more custom class.
Additionally there may be cases when insertString() isn't called but structure is updated e.g. when I call
protected void insert(int offset, ElementSpec data) method of DefaultStyledDocument.
Joined: Jan 28, 2010
For now, I coded it my way and it works perfectly. However I did not consider updates resulting from a call to the insert method. So my question is would I be fine if I avoid calling this method (which I am not) or there may be times that it's called without me knowing?