I want to share with you some code which I have recently finished, which gives respect to the game chat GUI. For the game I'm making, I needed some component, which would allow coloured text, content appending and removing when some limit of lines is reached and some scroll behaviour, which I'll post after I will implement it .

The chat has following aspect

So, as main container of the content I have chosen JTextPane, since it allows HTML(which make possible full personalization of the text, including colour, text styles and much more). But there were several problems and challenges to solve, like appending and removing the text because JTextPane doesn't have such options.

I'm using the following code for creation of the JTextPane, note that there should be set HTMLEditorKit and HTMLDocument to the JTextPane, else HTML will not be rendered(thanks for people from StackOverflow helping me solve this issue)

The next step is creating line limit and remove the lines from the start which pass the limit. For this step I have used LimitLinesDocumentListener taken from http://tips4java.wordpress.com/2008/10/15/limit-lines-in-document/ (read on the blog about how it's being used). Though I needed to do some changes in the removeLines function(which in my case will not use functions removeFromStart and removeFromEnd. And now it's looks like:

I hope this will help some1 and save some time implementing nice chat(console) with colours and appending/removing. If you will find some optimization issues in my code or some things which can be simplified I will be very thankful if you report them in this topic or via pm.

To append or remove strings from JTextPane, you use the StyledDocument by calling getStyledDocument() on the JTextPane object. StyledDocument has insertString() and remove()

Instead of using HTML, you create Styles by calling addStyle() in StyledDocument, which returns a Style object that you can customize using the StyleConstants class. Then whenever you need to insert a string, you give insertString() the string and the style you want to use.Example:

I have tried to work with StyledDocument.. I could get it showing correctly different colour line(different colors in the same line). That was the main reason why I started to work with HTML. Another thing, does Styled document will allow to be extended to show emotes and for example links to items?

Yes, images and JComponent's are supported, though you use the methods insertIcon() and insertComponent() in JTextPane.

I have looked everywhere how to add links and have still found nothing. The only way I can think of doing links is to add a JLabel to the text pane and have a mouseListener set on it that would process the click.

Sure! I have removed all the networkings and listeners to just show what I used to create the visual. I use 2 custom Styles: Names and Notifications. The Names is just to bold the name of the chatter. The Notifications is for system messages. This is an applet so that's why you see the init() and add() methods.

java-gaming.org is not responsible for the content posted by its members, including references to external websites,
and other references that may or may not have a relation with our primarily
gaming and game production oriented community.
inquiries and complaints can be sent via email to the info‑account of the
company managing the website of java‑gaming.org