Want more features on Pastebin? Sign Up, it's FREE!
Guest

Untitled

By: a guest on Dec 14th, 2011  |  syntax: None  |  size: 0.52 KB  |  views: 28  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. public void append(JTextPane j, Color c, String s) { // better implementation--uses
  2.                 // StyleContext
  3.                 StyleContext sc = StyleContext.getDefaultStyleContext();
  4.                 AttributeSet aset = sc.addAttribute(SimpleAttributeSet.EMPTY,StyleConstants.Foreground, c);
  5.  
  6.                 int len = j.getDocument().getLength(); // same value as
  7.                 // getText().length();
  8.                 j.setCaretPosition(len); // place caret at the end (with no selection)
  9.                 j.setCharacterAttributes(aset, false);
  10.                 j.replaceSelection(s); // there is no selection, so inserts at caret
  11.         }
  12.  
clone this paste RAW Paste Data