//获得当前java编辑器的文本编辑对象
//设置光标的位置
CompilationUnitEditor currenteditor = (CompilationUnitEditor) editorPart.getPages().get(1);
//获得文本编辑对象的document
IDocument document = currenteditor.getViewer().getDocument();
//根据字符串获取到字符的位置
position = document.get().indexOf("{",position)+1;
//将当前编辑器设为活动编辑器
editorPart.setActiveEditor(currenteditor);//设置光标的位置
currenteditor.selectAndReveal(position, 0);//这个0 就是选中光标位置后面的文本长度
//编辑器获得焦点
currenteditor.setFocus();
到此,就可以定位到java 文件中某个字符串中的位置
Java编辑器光标定位
本文介绍了一种方法,可以在Java编辑器中通过编程方式定位到特定字符串的位置,并设置光标。具体步骤包括:获取当前编辑器实例、文档对象、字符串位置、设置编辑器为活动状态、设置光标位置并使编辑器获得焦点。
1213

被折叠的 条评论
为什么被折叠?



