在JEditorPane中,我想搜索一个字符串,我知道scrollToReference不起作用,所以我想知道如何遍历窗格并找到指定的字符串。
如何遍历JEditorPane以在java中查找指定的字符串?
发布于 2012-11-22 09:51:34
我能想到的最简单的方法就是遍历文档。
Document document = editor.getDocument();
try {
String find = //... String to find
for (int index = 0; index + find.length() < document.getLength(); index++) {
String match = document.getText(index, find.length());
if (find.equals(match)) {
// You found me
}
}
} catch (BadLocationException ex) {
ex.printStackTrace();
}您可以根据这个概念编写一些例程来“查找下一个单词”
您还可以找到Highlight a word in JEditorPane和Java - Scroll to specific text inside JTextArea,它们都使用类似的方法来实现不同的功能
https://stackoverflow.com/questions/13504791
复制相似问题