Creating an advanced text log...

Hello everyone, I am trying to create a text log that updates throughout gameplay with meaningful information. I’ve already done this before using a JTextArea, but this time I want the log to display different fonts/colors/sizes, not just one font for the whole thing. I’ve spent hours trying to figure out how to do this with the JTextPane or JEditorPane but I am getting confused and frustrated trying to figure out if I need to have a seperate document etc… is there a simple way to do this???

Thanks!

This should get you started:


      JTextPane textPane = new JTextPane();
      StyledDocument doc = textPane.getStyledDocument();

      Style regular = StyleContext.getDefaultStyleContext().getStyle(StyleContext.DEFAULT_STYLE);

      Style style1 = doc.addStyle("someCode", regular);
      StyleConstants.setFontFamily(style1, "courier");
      StyleConstants.setFontSize(style1, 12);
      StyleConstants.setBold(style1, true);

      Style style2 = doc.addStyle("plainText", style1); // inherits from style1
      StyleConstants.setFontFamily(style2, "arial");
      StyleConstants.setFontSize(style2, 12);
      StyleConstants.setItalic(style2, true);


      doc.insertString(0, "hello world");
      doc.setCharacterAttributes(0, 11, doc.getStyle("plainText"), false);
//or  doc.setCharacterAttributes(0, 11, style1, false);
      doc.setCharacterAttributes(3, 5, doc.getStyle("someCode"), false);
//or  doc.setCharacterAttributes(3, 5, style2, false);

“hello world” will be bold
“lo wo” will be both bold and italic

Be sure to run ALL CODE from the EDT, because JTextPane is VERY unreliable when accessed from other threads.

Thanks, this will work fine! If the document object gets very large (like 1000+ lines of text), will the program slow down noticeably each time i add text and reset the document in the textPane?

Try :persecutioncomplex:

For this case I would actually just set up JList (or something similiar) to take HTML code. Then you can use simple <b> tags to bold things out, set colors with the <font> tag… bla bla bla

You have to remember that the parser is compatible up to HTML 3.2, hence having to use <font> to set the color…

code snippet:


Stack logData;
JList log;
JScrollPane logScroll;
public Constructor() {
  logData = new Stack();
  log = new JList();
  log.setEnabled(false);
  logScroll = new JScrollPane(log);
}
public void log(String msg) {
  logData.push(msg);
  log.setListData(logData);
  Rectangle r = log.getBounds();
  if (r.height >= logScroll.getBounds().height) {
    /* scrolls the log down to the newest item */
    logScroll.getViewport().setViewPosition(new Point(0,r.height));
  }
}
public void clear() {
  logData.clear();
  log.setListData(logData);
}

Then you can just log HTML code…


log("<html><font color=blue>This will be blue");

That way you don’t have to worry about using those Style objects and such…

The above code is taken from a template GUI I use whenever I make a server-client app. The result looks decent:

http://woogley.net/misc/log.jpg