跳到JEditorPane的内部锚点

时间:2012-12-27 12:20:12

标签: java html swing hyperlink jeditorpane

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

窗格的内容不是从资源加载的,而是动态创建的,并以字符串形式提供。

如何让我的JEditorPane滚动到正确的位置? (在示例中,它应滚动到顶部) 侦听器只捕获null,这会增加问题。

这是我的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);
}

2 个答案:

答案 0 :(得分:8)

好的,我终于解决了这个问题。

我一直在测试scrollToReference(),但它在某种程度上无效。 然后我玩了HTML解析和锚点和插入符号和setCaretPosition(),它们只在某些时候起作用。 然后出于纯粹的巧合,我在我的代码中再次包含scrollToReference()并且滚动突然工作......并且仍然完美无瑕地工作!

这是工作代码:

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

答案 1 :(得分:1)

你可以这样做:

import java.awt.Point;
import java.util.logging.Logger;
import javax.swing.JEditorPane;
import javax.swing.JFrame;
import javax.swing.JScrollPane;
import javax.swing.JViewport;
import javax.swing.SwingUtilities;
import javax.swing.event.HyperlinkEvent;
import javax.swing.event.HyperlinkListener;

public class PaneWithScroll {
  private static final String TEXT = 
      "<html>"
        + "<head>"
        + "</head>"
        + "<body>"
          + "<p><a href=\"#top\">Go top</a></p>"
        + "</body>"
      + "</html>";
  private static final String TOP = "#top";

  public static void main(String[] args) {
    SwingUtilities.invokeLater(new Runnable() {
      @Override
      public void run() {
        JFrame frame = new PaneWithScrollFrame();
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setVisible(true);
      }
    });
  }

  private static class PaneWithScrollFrame extends JFrame {
    private PaneWithScrollFrame() {
      super.setName("Pane With Scroll");
      this.addComponents();
      super.setSize(640, 480);
    }

    private void addComponents() {
      JEditorPane editorPane = new JEditorPane();
      editorPane.setContentType("text/html");
      editorPane.setEditable(false);
      editorPane.setText(TEXT);

      final JScrollPane scrollpane = new JScrollPane(editorPane);

      editorPane.addHyperlinkListener(new HyperlinkListener() {
        @Override
        public void hyperlinkUpdate(HyperlinkEvent e) {
          if (HyperlinkEvent.EventType.ACTIVATED.equals(e.getEventType())) {
            String description = e.getDescription();
            if (TOP.equals(description)) {
              JViewport viewport = scrollpane.getViewport();
              viewport.setViewPosition(new Point(0, 0));
            }
          }
        }
      });

      super.add(scrollpane);
    }
  }
}