我正在尝试突出JTextPane
中的特定行。假设我要突出显示JTextPane
的第5行,如果行相同,如何让indexOf
突出显示它?
JTextPane
的示例内容,我想从下面的行中突出第5行和第11行,
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);
}
答案 0 :(得分:4)
1)hilit.removeAllHighlights();
在所有情况下都无法正常工作,您已填写arrays of Highlighter[],
2)你从Document
,tutorial talking about searching in the Document提取Model for JTextComponents
JTextComponents
,然后你can styled text into JTextPane
(我在说话)关于最简单的方法,有多种方法可以确定具体行中的内容,这些事情可能会使JTextComponents的大小调整复杂化)
3)我对Columns
Rows
的{{1}}和JTextComponents
留下了答案{/ 1}}
答案 1 :(得分:3)
我猜你的模型是HTMLDocument
,这不是真正面向行的。或者,使用JList
考虑JTextPane
和自定义渲染器。