Articles Projects Tips Downloads Contacts About

JEditorPane's content height for fixed width.

To provide correct layout of JEditorPane's content we need to know what height we will have for the content if JEditorPane's width is fixed. The code below can be used to measure JEditorPane and JTextPane.

    public static int getContentHeight(String content) {
        JEditorPane dummyEditorPane=new JEditorPane();
        dummyEditorPane.setSize(100,Short.MAX_VALUE);
        dummyEditorPane.setText(content);
        
        return dummyEditorPane.getPreferredSize().height;
    }

    int h=getContentHeight("Long text to be measured in the JEditorPane");
    System.out.println("height="+h);