public static void setText ( JTextComponent textComp , String text ) { if ( text == null ) text = "" ; if ( textComp . getCaret ( ) instanceof DefaultCaret ) { DefaultCaret caret = ( DefaultCaret ) textComp . getCaret ( ) ; int updatePolicy = caret . getUpdatePolicy ( ) ; caret . setUpdatePolicy ( DefaultCaret . NEVER_UPDATE ) ; try { textComp . setText ( text ) ; } finally { caret . setUpdatePolicy ( updatePolicy ) ; } } else { int mark = textComp . getCaret ( ) . getMark ( ) ; int dot = textComp . getCaretPosition ( ) ; try { textComp . setText ( text ) ; } finally { int len = textComp . getDocument ( ) . getLength ( ) ; if ( mark > len ) mark = len ; if ( dot > len ) dot = len ; textComp . setCaretPosition ( mark ) ; if ( dot != mark ) textComp . moveCaretPosition ( dot ) ; } } }
sets text of textComp without moving its caret .