问题
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