我正在尝试使活动行的项目符号具有突出显示的背景。我正在使用
int activeLine = styledText.getLineAtOffset(styledText.getCaretOffset());来获得目前正在使用的类似功能。这似乎是有效的,除非我按回车键并获得一个新的行。

getCaretOffset返回35,getCharCount返回36。
但是,如果我单击最后一行(目前我在单击时调用redraw() ),该行将正确突出显示,并且getCaretOffset返回36。

下面是相关的代码
public void lineGetStyle(LineStyleEvent event) {
// Set the line number
int activeLine = styledText.getLineAtOffset(styledText.getCaretOffset());
System.out.println("Offset " + styledText.getCaretOffset() + " max " + styledText.getCharCount());
int currentLine = styledText.getLineAtOffset(event.lineOffset);
event.bulletIndex = currentLine;
// Set the style, 12 pixles wide for each digit
StyleRange style = new StyleRange();
style.metrics = new GlyphMetrics(0, 0, 36);
if (activeLine == currentLine) {
style.background = highlightedLine;
if (curActiveLine != activeLine){
System.out.println("ActiveLine " + activeLine + " old " + curActiveLine);
int redrawLine = curActiveLine;
curActiveLine = activeLine;
styledText.redraw(0, styledText.getLinePixel(redrawLine), 36, styledText.getLineHeight(),true);
}
}
style.foreground = mainBackground;
// Create and set the bullet
event.bullet = new Bullet(ST.BULLET_NUMBER, style);
event.styles = matchKeywords(event);
}发布于 2013-06-13 02:04:01
我刚刚意识到,您可以设置一个CaretListener来获取插入符号每次移动时的通知。通过发出重绘,它现在工作得很好。
https://stackoverflow.com/questions/17072110
复制相似问题