2012-08-15 58 views
4

我无法弄清楚JEditorPane.scrollToReference()是如何用于动态生成的HTML页面的。我想打开一个JEditorPane对话框,滚动到我选择的锚点。无论我如何解决问题,视图总是在页面底部滚动。带动态生成HTML的JEditorPane.scrollToReference()

作为一个丑陋的解决办法,我现在解析为锚标签的完整的HTML文档,保存他们的偏移量在Map<String, Integer>,然后我打电话:

editorPane.setCaretPosition(anchorMap.get("anchor-name")); 

...这甚至不产生吸引力结果,因为可见矩形内的插入符号位置看起来不可预知,并且很少位于窗口顶部。我正在寻找更类似于浏览器的行为,其中锚定文本出现在可见区域的顶部。

下面是什么我尴尬的解决方法发生截图(上有“头6”锚),而不必触及滚动条:

screenshot http://i49.tinypic.com/jkaj9s.png

我想我失去了一些东西但我无法弄清楚究竟是什么。

我目前的解决方法来源:

import javax.swing.*; 
import javax.swing.text.MutableAttributeSet; 
import javax.swing.text.html.*; 
import javax.swing.text.html.HTMLEditorKit.*; 
import javax.swing.text.html.parser.*; 
import java.io.*; 
import java.awt.*; 
import java.util.HashMap; 

public class AnchorTest 
{ 
    public static void main(String[] args) 
    { 
     final String html = generateLongPage(); 
     final HashMap<String, Integer> anchors = anchorPositions(html); 

     SwingUtilities.invokeLater(new Runnable() 
     { 
      public void run() 
      { 
       JEditorPane editor = new JEditorPane("text/html", html); 
       JScrollPane scroller= new JScrollPane(editor); 

       scroller.setPreferredSize(new Dimension(400, 250)); 

       //editor.scrollToReference("anchor6"); // doesn't work... 
       editor.setCaretPosition(anchors.get("anchor6")); //sorta works 

       JOptionPane.showMessageDialog(new JPanel(), scroller, "", 
         JOptionPane.PLAIN_MESSAGE); 
      }}); 
    } 

    public static HashMap<String, Integer> anchorPositions(String html) 
    { 
     final HashMap<String, Integer> map = new HashMap<String, Integer>(); 

     Reader reader = new StringReader(html); 
     HTMLEditorKit.Parser parser = new ParserDelegator(); 

     try 
     { 
      ParserCallback cb = new ParserCallback() 
      { 
       public void handleStartTag(HTML.Tag t, 
              MutableAttributeSet a, 
              int pos) 
       { 
        if (t == HTML.Tag.A) { 
         String name = 
          (String)a.getAttribute(HTML.Attribute.NAME); 
         map.put(name, pos); 
        } 
       } 
      }; 

      parser.parse(reader, cb, true); 
     } 
     catch (IOException ignore) {} 

     return map; 
    } 


    public static String generateLongPage() 
    { 
     StringBuilder sb = new StringBuilder(
       "<html><head><title>hello</title></head><body>\n"); 

     for (int i = 0; i < 10; i++) { 
      sb.append(String.format(
        "<h1><a name='anchor%d'>header %d</a></h1>\n<p>", i, i)); 

      for (int j = 0; j < 100; j++) { 
       sb.append("blah "); 
      } 
      sb.append("</p>\n\n"); 
     } 

     return sb.append("</body></html>").toString(); 
    } 
} 

回答

5

这个问题可能是因为面板尚不可见或不执行时scrollToReference又摆出来,所以无法确定其大小。

scrollToReference的实施方案具有下列块:

Rectangle r = modelToView(iter.getStartOffset()); 
if (r != null) { 
    ... 
    scrollRectToVisible(r); 
} 

在张贴示例的矩形是为nullanchor6,由于视图尚未可见或它的大小不为正。

试试这个肮脏的修复延迟滚动:

SwingUtilities.invokeLater(new Runnable() { 
    public void run() { 
     editor.scrollToReference("anchor6"); 
    } 
}); 
JOptionPane.showMessageDialog(new JPanel(), scroller, "", 
     JOptionPane.PLAIN_MESSAGE); 
+0

我有同样的想法,你,但它并没有区别,当我尝试过。你有不同的结果吗? – 2012-08-15 21:41:14

+0

@NickRippe是的,它滚动确定。 – tenorsax 2012-08-15 21:43:01

+0

啊,是的 - 现在我可以开始工作。 'showMessageDialog'和'scrollToReference'都需要位于'invokeLater'语句中(按此顺序)。我一定有一些东西被弄糊涂了。 +1 – 2012-08-15 21:57:08