java JTextPane 突出显示文本
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/5674128/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
JTextPane highlight text
提问by xdevel2000
Can I highlight some text into a JTextPane
starting from a value and ending from another value
like the following but with the yellow color?
我可以将一些文本突出显示为JTextPane
从一个值开始并从另一个值结束,如下所示,但颜色为黄色吗?
"" JTextPane highlighttext ""
""JTextPane突出显示文本""
Thanks.
谢谢。
回答by kleopatra
As often there are several possibilities, depending on what you really mean by "highlight":-)
通常有几种可能性,这取决于“突出显示”的真正含义:-)
Highlight by changing any style attributes of arbitrary text parts on the document level, something like
通过在文档级别更改任意文本部分的任何样式属性来突出显示,例如
SimpleAttributeSet sas = new SimpleAttributeSet();
StyleConstants.setForeground(sas, Color.YELLOW);
doc.setCharacterAttributes(start, length, sas, false);
Highlight via a Highlighter on the textPane level:
通过 textPane 级别的 Highlighter 突出显示:
DefaultHighlighter.DefaultHighlightPainter highlightPainter =
new DefaultHighlighter.DefaultHighlightPainter(Color.YELLOW);
textPane.getHighlighter().addHighlight(startPos, endPos,
highlightPainter);
回答by Makky
JTextArea textComp = new JTextArea();
// Highlight the occurrences of the word "public"
highlight(textComp, "public");
// Creates highlights around all occurrences of pattern in textComp
public void highlight(JTextComponent textComp, String pattern)
{
// First remove all old highlights
removeHighlights(textComp);
try
{
Highlighter hilite = textComp.getHighlighter();
Document doc = textComp.getDocument();
String text = doc.getText(0, doc.getLength());
int pos = 0;
// Search for pattern
// see I have updated now its not case sensitive
while ((pos = text.toUpperCase().indexOf(pattern.toUpperCase(), pos)) >= 0)
{
// Create highlighter using private painter and apply around pattern
hilite.addHighlight(pos, pos+pattern.length(), myHighlightPainter);
pos += pattern.length();
}
} catch (BadLocationException e) {
}
}
// Removes only our private highlights
public void removeHighlights(JTextComponent textComp)
{
Highlighter hilite = textComp.getHighlighter();
Highlighter.Highlight[] hilites = hilite.getHighlights();
for (int i=0; i<hilites.length; i++)
{
if (hilites[i].getPainter() instanceof MyHighlightPainter)
{
hilite.removeHighlight(hilites[i]);
}
}
}
// An instance of the private subclass of the default highlight painter
Highlighter.HighlightPainter myHighlightPainter = new MyHighlightPainter(Color.red);
// A private subclass of the default highlight painter
class MyHighlightPainter extends DefaultHighlighter.DefaultHighlightPainter
{
public MyHighlightPainter(Color color)
{
super(color);
}
}
回答by clamp
Yes you can via the functions setSelectionStartand setSelectionEndfrom JTextComponentwhich JTextPaneinherits from.
是的,你可以通过功能setSelectionStart和setSelectionEnd从JTextComponent的其中的JTextPane从继承。
回答by John
Did you try java's string comparison method
你有没有试过java的字符串比较方法
.equalsIgnoreCase("Search Target Text")
Because this method allows a search without having to take the case of a string into account This might be the ticket to what you are trying to achieve
因为此方法允许搜索而不必考虑字符串的大小写 这可能是您尝试实现的目标
Hope this helps you Makky
希望这对你有帮助
回答by user667522
performance wise its better to put the toUpperCase on
性能明智最好将 toUpperCase 放在
String text = doc.getText(0, doc.getLength());
String text = doc.getText(0, doc.getLength());
rather than in the while loop
而不是在 while 循环中
but thanks for the good example.
但感谢你的好例子。