突出显示jtextpane中的特定行 [英] highlighting a specific line from jtextpane

查看:147
本文介绍了突出显示jtextpane中的特定行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试突出显示来自 JTextPane 的特定行。假设我要突出显示 JTextPane 中的第5行,如何获取 indexOf 如果线条突出显示它是一样的吗?

I am trying to highlight a specific lines from JTextPane. Suppose I want to highlight the 5th line from JTextPane, how do I get the indexOf it to highlight it if the lines are same?

JTextPane 的示例内容,我希望从下面的行中突出第5和第11行,

Example content of JTextPane, I want to higlight 5th and 11th line from below lines,


This text is from stackoverflow
This text is from stackoverflow
This text is from stackoverflow
This text is from stackoverflow
This text is from stackoverflow
This text is from stackoverflow
This text is from stackoverflow
This text is from google
This text is from yahoo
This text is from yahoo
This text is from yahoo
This text is from yahoo

代码:

//Code to highlight
//text is jtextpane
final static Color HILIT_COLOR = Color.LIGHT_GRAY;
DefaultHighlighter hilit = new DefaultHighlighter();
DefaultHighlightPainter painter = new  
    DefaultHighlighter.DefaultHighlightPainter(HILIT_COLOR);
text.setHighlighter(hilit);

hilit.removeAllHighlights();
String s = text.getText();
try {
    hilit.addHighlight(0, 10, painter);
} catch (BadLocationException ex) {
    Logger.getLogger(TextLines.class.getName()).log(Level.SEVERE, null, ex);
}


推荐答案

1) hilit.removeAllHighlights(); 在所有情况下都无法正常工作,你已经填写了数组荧光笔[]

1) hilit.removeAllHighlights(); doens't works correctly in all of cases, you have fill arrays of Highlighter[],

2)你有提取文件模型for JTextComponents )来自 JTextComponents 教程谈论在文档中搜索,然后你可以将文本设置为 JTextPane (我说的是最简单的方法,有多种方法可以确定具体行中的内容,这些可能会使JTextComponents的大小变得复杂)

2) you have extract Document (Model for JTextComponents) from JTextComponents, tutorial talking about searching in the Document, then you can styled text into JTextPane(I'm talking about easiest way, there are ways how to determine contents in the concrete row(s), those things could be complicating the resize of JTextComponents)

3)我的答案是关于来自 JTextComponents @Stanislav

3) I leaving answer about Columns and Rows from JTextComponents for @Stanislav

这篇关于突出显示jtextpane中的特定行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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