在JTextPane中选择多个项目 [英] Select multiple items in JTextPane

查看:104
本文介绍了在JTextPane中选择多个项目的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想突出显示(选择)JTextPane中多次出现的字符串.我想做些类似findall的东西.这是我写的.

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.

推荐答案

我认为有关 JTextComponents 包含有关该内容的详细说明

I think that tutorial about JTextComponents contains detailed descriptions about that

  • 您已经从<<中提取了文档 a href ="http://docs.oracle.com/javase/tutorial/uiswing/components/jcomponent.html" rel ="nofollow noreferrer"> JTextComponents ,

  • you have extract Document from JTextComponents,

然后您可以将文本设置为 JTextPane ,基本上有两种使用 Highlighter和StyledDocument的方式

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

对于HTML格式的文本,您必须创建自己的

for Html formatted text, you have to create own HtlmEditorKit

这篇关于在JTextPane中选择多个项目的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆