private static void onReturnPressed(KeyEvent event) throws BadLocationException {
  JEditorPane editor=(JEditorPane)event.getSource();
  Document doc=editor.getDocument();
  int pos=editor.getCaretPosition();
  String indent=getIndent(doc,pos);
  Element root=doc.getDefaultRootElement();
  Element elem=root.getElement(root.getElementIndex(pos));
  int start=elem.getStartOffset();
  String line=doc.getText(start,pos - start);
  if (isStartElement(line)) {
    indent+="\t";
  }
  doc.insertString(pos,"\n" + indent,null);
  event.consume();
}

private static void onReturnPressed ( KeyEvent event ) throws BadLocationException { JEditorPane editor = ( JEditorPane ) event . getSource ( ) ; Document doc = editor . getDocument ( ) ; int pos = editor . getCaretPosition ( ) ; String indent = getIndent ( doc , pos ) ; Element root = doc . getDefaultRootElement ( ) ; Element elem = root . getElement ( root . getElementIndex ( pos ) ) ; int start = elem . getStartOffset ( ) ; String line = doc . getText ( start , pos - start ) ; if ( isStartElement ( line ) ) { indent += "\t" ; } doc . insertString ( pos , "\n" + indent , null ) ; event . consume ( ) ; }
reacts to a pressed return . simply goes to the next line with the proper indentation .
