Java Default Highlighter

人盡茶涼 提交于 2019-12-02 05:33:06

问题


Im using the DefaultHightlighter.DefaultHightlighterPainter to highlight text within a java text pane. I want to remove all highlights (there could be more than one string highlighted) and want it to return the locations of the strings where the highlight has been removed, so obviously I cant use pseudoCodeTextPane.getHighlighter().removeHighlight(highlight);

Can anyone help? Thanks


回答1:


How about something like

 Highlighter.Highlight[] highlights = pseudoCodeTextPane.getHighlighter().getHighlights();
 int[] startOffsets = new int[highlights.length];
 int[] endOffsets = new int[highlights.length];
 for (int i = 0; i < highlights.length; ++i) {
     startOffsets[i] = highlights[i].getStartOffset();
     endOffsets[i] = highlights[i].getEndOffset();
 }
 pseudoCodeTextPane.getHighlighter().removeAllHighlights();
 // now do whatever processing you want to do with the highlight locations



回答2:


If you remove all highlights (I suppose with removeAllHighlights) you can getHighlights before that and use the information you receive there.



来源:https://stackoverflow.com/questions/9078275/java-default-highlighter

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!