File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Smilies in JTextPane

 
marwen Bakkar
Ranch Hand
Posts: 84
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

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?

Thanks in advance.
 
Stanislav Lapitsky
Ranch Hand
Posts: 53
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
http://forums.sun.com/thread.jspa?threadID=5427306&tstart=0
Don't crosspost.

See my answer on sun's forums.

Regards,
Stas
 
Maneesh Godbole
Saloon Keeper
Posts: 10971
11
Android Eclipse IDE Google Web Toolkit Java Mac Ubuntu
 
marwen Bakkar
Ranch Hand
Posts: 84
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I was unaware of this, sorry.

Thanks Stanislav Lapitsky.
 
marwen Bakkar
Ranch Hand
Posts: 84
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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?
 
Stanislav Lapitsky
Ranch Hand
Posts: 53
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.

Regards,
Stas
 
marwen Bakkar
Ranch Hand
Posts: 84
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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?
 
Stanislav Lapitsky
Ranch Hand
Posts: 53
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If you use StyledEditorKit I see no big difference.

Regards,
Stas
 
marwen Bakkar
Ranch Hand
Posts: 84
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic