Select multiple items in JTextPane

放肆的年华 提交于 2019-12-24 05:44:06

问题


I would like to highlight(select) multiple occurrences of a string in JTextPane. I would like to make something like findall. Here is what I have written.

 int a=0;
 while(jTextPane1.getText().indexOf(search,a)>0)
 {
     int i =jTextPane1.getText().indexOf(search,a);
     a=i+search.length();
     jTextPane1.select(i,a);
 }

It works ok, but the problem is that it highlights only the last occurrence, because the highlight changes. I would like to make multiple highlights.


回答1:


I think that tutorial about JTextComponents contains detailed descriptions about that

  • you have extract Document from JTextComponents,

  • then you can styled text into JTextPane, basically there are two ways use Highlighter and StyledDocument

  • for Html formatted text, you have to create own HtlmEditorKit



来源:https://stackoverflow.com/questions/9878085/select-multiple-items-in-jtextpane

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!