JTextPane HTML renderer wrong?

隐身守侯 提交于 2020-01-06 15:29:10

问题


I have a JTextPane and want to create a div within it, with exactly the height of the JTextPane. The div should be always the height of the JTextPane.

public class Test {

JTextPane edit=new JTextPane();
public Test() {
    JFrame frame=new JFrame("Center text vertically");
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.getContentPane().add(edit);
    String text = "<html><div style=\"height: 0px; border: 1px solid black;\">here is some text</div></html>";
    edit.setEditorKit(new HTMLEditorKit());
    edit.setContentType("text/html");
    edit.setText(text);

    edit.addComponentListener(new ComponentAdapter() {
        @Override
        public void componentResized(ComponentEvent e) {
            int height = edit.getHeight();
            height -= 50; //this makes the problem more clear
            String text = "<html><div style=\"height: " + height + "px; border: 1px solid black;\">here is some text</div></html>";
            edit.setText(text);
        }
    });
    frame.setSize(300,200);
    frame.setLocationRelativeTo(null);
    frame.setVisible(true);
}
public static void main(String[] args) throws Exception {
    new Test();
}
}

Whenever the JTextPane resizes, I take the height of the JTextPane and create a div with exactly this height.

The problem is, that the div is sometimes larger than the height and sometimes smaller than the height. You can try this by changing the height of the frame. I thought that the div should equally scale with the JTextPane, however it does not...

Note: The height of the JTextPane is calculated correctly (double checked with Gimp).

Any ideas?


回答1:


Any ideas?

Abandon pursuing the DIV idea, and instead set the style to the BODY element. The body will fill the available space unless the content is too long to fit, then it will overflow.



来源:https://stackoverflow.com/questions/21131748/jtextpane-html-renderer-wrong

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!