本文介绍了跳到JEditorPane中的内部锚点的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我遇到了一个问题:我想在JEditorPane中使用内部锚点<a name="x">和链接<a href="#x">.

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.

如何使JEditorPane滚动到正确的位置? (在示例中,它应该滚动到顶部)侦听器仅捕获null,这增加了问题.

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.

这是我的SSCCCE:

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);
}

推荐答案

好吧,我终于解决了这个问题.

Okay I finally got this solved.

我一直在使用scrollToReference()进行测试,但是以某种方式无法正常工作.然后,我玩了HTML解析,锚点和插入符以及setCaretPosition(),它们有时仅起作用.然后出于纯粹的巧合,我再次在代码中包含了scrollToReference(),滚动突然起作用了……而且仍然可以完美地工作!

I had been testing around with scrollToReference(), but it somehow didn't work.Then I played with HTML parsing and anchors and carets and setCaretPosition() which worked only sometimes.Then out of pure coincidence I had included scrollToReference() again in my code and scrolling suddenly worked... and still works flawlessly!

这是工作代码:

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 /><a href='#thisisbottom'>down</a><br /><br /><br /><br /><br /><br /><br /><br /><br /><a name='mid1'></a>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>] <br /> or jump to <a name='thisisbottom' href='#mid1'>center</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("JEditorPane link click: url='" + pE.getURL() + "' description='" + pE.getDescription() + "'");
                String reference = pE.getDescription();
                if (reference != null && reference.startsWith("#")) { // link must start with # to be internal reference
                    reference = reference.substring(1);
                    ep.scrollToReference(reference);
                }
            }
        }
    });

    final JScrollPane sp = new JScrollPane(ep);
    f.add(sp);

    f.setBounds(200, 200, 400, 400);
    f.setVisible(true);
}

这篇关于跳到JEditorPane中的内部锚点的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-20 03:39