Hello,
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
http://game2dei.com/upload/2011/02/console.png
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)
this.text_panel = new JTextPane();
this.text_panel.setContentType("text/html");
this.text_panel.setEditable(false);
this.text_panel.setBackground(this.text_background_color);
this.text_panel_html_kit = new HTMLEditorKit();this.text_panel.setEditorKit(text_panel_html_kit);
this.text_panel.setDocument(new HTMLDocument());
scroll_pane = new JScrollPane(this.text_panel);
scroll_pane.setBounds(5,5,580,110);
scroll_pane.setHorizontalScrollBarPolicy(ScrollPaneConstants.HORIZONTAL_SCROLLBAR_NEVER);
scroll_pane.setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS);
this.add(scroll_pane);
The next step was creating the append function. You will need to work directly with the HTMLDocument set to the JTextPane in previous step. My append function looks like:
public void append(String line){
SimpleDateFormat date_format = new SimpleDateFormat("HH:mm:ss");
Date date = new Date();
line = "<div><font size=3 color=GRAY>[" + date_format.format(date) + "]</font><font size=3 color=BLACK>"+ line + "</font></div>";
try {
this.text_panel_html_kit.insertHTML((HTMLDocument) this.text_panel.getDocument(), this.text_panel.getDocument().getLength(), line, 0, 0, null);
} catch (Exception e) {
e.printStackTrace();
}
}
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:
private void removeLines(DocumentEvent e)
{
Document document = e.getDocument();
try {
while (((HTMLDocument) document).getText(0, document.getLength()).split("\\n").length > maximumLines)
{
int end = document.getText(0, document.getLength()).split("\\n")[0].length();
document.remove(0, end+1);
}
} catch (BadLocationException e1) {
e1.printStackTrace();
}
}
The last step is adding the listener to the text_panel, this is done with following code:
LimitLinesDocumentListener limitLinesListener = new LimitLinesDocumentListener(Configuration.getInstance().CHAT_VISIBLE_LINES);
this.text_panel.getDocument().addDocumentListener(limitLinesListener);
And before finishing note, there was also added some scroll behaviour, which makes the scroll jump to the last inserted item, achieved with following code:
DefaultCaret caret = (DefaultCaret)text_panel.getCaret();
caret.setUpdatePolicy(DefaultCaret.ALWAYS_UPDATE);
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.
Thanks for attention