I've got a problem: I want to use internal anchors <a name="x">
and links <a href="#x">
inside a JEditorPane.
The content of the pane is not loaded from a resource but dynamically created and available as a String.
How can I get my JEditorPane to scroll to the proper location? (in the example it should scroll to the top) The listener only catches null, which adds to the problem.
Here's my SSCCCE:
public static void main(final String[] args) {
final JFrame f = new JFrame();
f.setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
f.setTitle("JEditorPane Test");
final String text = "<html><body><a name='link1'>test</a>some text<br /><br /><br /><br /><br /><br /><br /><br /><br /><br />some more text<br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br />some more text<a href='#link1'>jump to top</a></body></html>";
final JEditorPane ep = new JEditorPane();
ep.setContentType("text/html");
ep.setText(text);
ep.setEditable(false);
ep.addHyperlinkListener(new HyperlinkListener() {
@Override public void hyperlinkUpdate(final HyperlinkEvent pE) {
if (HyperlinkEvent.EventType.ACTIVATED == pE.getEventType())
System.out.println("ep link click: " + pE.getURL());
}
});
final JScrollPane sp = new JScrollPane(ep);
f.add(sp);
f.setBounds(200, 200, 400, 400);
f.setVisible(true);
}