Java scrollToReference导致JEditorPane中出现异常
我在JScrollPane中有一个带有自定义html文档(不是来自URL)的JEditorPane,还有一个JTextField,用户可以输入文本,然后在编辑器窗格中高亮显示。在textfield的keyPressed事件中,我在文档中搜索文本,并用以下内容将其包围:Java scrollToReference导致JEditorPane中出现异常,java,swing,jeditorpane,Java,Swing,Jeditorpane,我在JScrollPane中有一个带有自定义html文档(不是来自URL)的JEditorPane,还有一个JTextField,用户可以输入文本,然后在编辑器窗格中高亮显示。在textfield的keyPressed事件中,我在文档中搜索文本,并用以下内容将其包围: <a name='spot'><span style='background-color: silver'>my text</span></a> 此调用在BoxView.mode
<a name='spot'><span style='background-color: silver'>my text</span></a>
此调用在BoxView.modelToView内引发ArrayIndexOutOfBoundsException。该方法在文本中找到我的“spot”引用,但我认为视图可能还没有用新文本更新,所以当它尝试滚动到那里时,它失败了
我找不到视图的引用,也找不到一个事件来监听,这意味着绝地武士窗格的视图已经完全更新。有什么想法吗
谢谢
Jared谈论URL的字符串引用
Scrolls the view to the given reference location (that is, the value
returned by the UL.getRef method for the URL being displayed).
然后,所有示例都显示了以下解决方法
import java.io.IOException;
import java.net.URL;
import javax.swing.JDialog;
import javax.swing.JEditorPane;
import javax.swing.JFrame;
import javax.swing.JScrollPane;
import javax.swing.event.HyperlinkEvent;
import javax.swing.event.HyperlinkListener;
public class MyScrollToReference extends JDialog {
private static final long serialVersionUID = 1L;
public MyScrollToReference(JFrame frame, String title, boolean modal, String urlString) {
super(frame, title, modal);
try {
final URL url = MyScrollToReference.class.getResource(urlString);
final JEditorPane htmlPane = new JEditorPane(url);
htmlPane.setEditable(false);
JScrollPane scrollPane = new JScrollPane(htmlPane);
getContentPane().add(scrollPane);
htmlPane.addHyperlinkListener(new HyperlinkListener() {
public void hyperlinkUpdate(HyperlinkEvent e) {
if (e.getEventType() == HyperlinkEvent.EventType.ACTIVATED) {
if (e.getURL().sameFile(url)) {
try {
htmlPane.scrollToReference(e.getURL().getRef());
} catch (Throwable t) {
t.printStackTrace();
}
}
}
}
});
} catch (IOException e) {
}
}
}
从哪种类型的HtmlEditorKit,最好使用引起的
ArrayIndexOutOfBoundsException更新您的问题
我没有更改任何HtmlEditorKit,因此无论JEditorPane的默认设置是什么。没有SSCCE,我无法找到您的问题,抱歉,电池未包括在内,也许有人可以…传递给方法的参数应该引用html文档中锚的“name”属性。我调用方法的方式不是问题所在。我之所以知道这一点,是因为我在调试器中遍历了该方法,并且它正确地定位了我的引用锚点。另外,我自己搜索了引用,然后调用了scrollRectToVisible,同样的错误发生了,我帮不了你更多,从未使用过,
import java.io.IOException;
import java.net.URL;
import javax.swing.JDialog;
import javax.swing.JEditorPane;
import javax.swing.JFrame;
import javax.swing.JScrollPane;
import javax.swing.event.HyperlinkEvent;
import javax.swing.event.HyperlinkListener;
public class MyScrollToReference extends JDialog {
private static final long serialVersionUID = 1L;
public MyScrollToReference(JFrame frame, String title, boolean modal, String urlString) {
super(frame, title, modal);
try {
final URL url = MyScrollToReference.class.getResource(urlString);
final JEditorPane htmlPane = new JEditorPane(url);
htmlPane.setEditable(false);
JScrollPane scrollPane = new JScrollPane(htmlPane);
getContentPane().add(scrollPane);
htmlPane.addHyperlinkListener(new HyperlinkListener() {
public void hyperlinkUpdate(HyperlinkEvent e) {
if (e.getEventType() == HyperlinkEvent.EventType.ACTIVATED) {
if (e.getURL().sameFile(url)) {
try {
htmlPane.scrollToReference(e.getURL().getRef());
} catch (Throwable t) {
t.printStackTrace();
}
}
}
}
});
} catch (IOException e) {
}
}
}