javaswingjeditorpane

How to set fixed width but dynamic height on JTextPane with HTML content type?


I have a JEditorPane. I want the pane height correspond to the content. I have done that and it works when the pane content is text:

public class JEditorPaneTest extends JFrame {
    
    public static void main(String[] args) {
        final JFrame mainFrame = new JFrame("test");
        mainFrame.getContentPane().setLayout(new FlowLayout());
        mainFrame.setSize(500,500);
            
        final JEditorPane field = new JEditorPane();
        //field.setContentType("text/html");
        field.setText(htmlize("Lorem ipsum dolor sit amet, consectetur adipiscing elit. Suspendisse accumsan magna vel libero bibendum, quis hendrerit nisi rutrum. Cras placerat erat eget dictum ornare. Sed eget nisl quis nibh vehicula mollis. Vestibulum non iaculis erat, quis pulvinar magna. Suspendisse ac rhoncus purus. Quisque finibus, dolor varius tincidunt aliquet, mauris felis condimentum neque, at viverra felis nulla at justo. Duis ut dui velit. Integer vitae mollis leo. Cras quis urna odio. Suspendisse tempus, urna sed maximus fringilla, ante velit finibus massa, id commodo libero quam non ipsum. Sed id augue vitae sapien sagittis imperdiet in eget nibh. Nam semper posuere nisl, dictum efficitur ipsum aliquet ac. Phasellus eros massa, fringilla et neque maximus, pretium tempor magna."));
        
        field.setSize(200,40);
        field.setSize(200, field.getPreferredSize().height);
                
        mainFrame.getContentPane().add(field,BorderLayout.CENTER);
        mainFrame.setVisible(true);
        System.out.println(field.getPreferredSize().width+" , "+field.getPreferredSize().height);
    }
    
    public static String htmlize(String pMessage) {
        return "<html><body>" + pMessage + "</body></html>";
    }
}

The preferred size is known according to the text content. The width is fixed to 200 so the preferred height is easily calculated by swing. In the console I can see 200, 411 (the width I fixed the the preferred height). In the dialog we see that the pane has the good size:

enter image description here

The problem is that I want an HTML content. So in the code I uncomment the line field.setContentType("text/html");

I run my program and I have in the console: 5393, 23: the width is not forced to 200 anymore.

Here the dialog result:

enter image description here

How can I resolve my problem with HTML content?


Solution

  • I think you should read again about layout managers.

    You set the layout manager for the content pane of the JFrame to FlowLayout and then you add the JEditorPane to it using BorderLayout. I don't know what that actually does but I don't think it is what you intended.

    FlowLayout respects the preferred size of the components it contains, so I set the preferred size of the JEditorPane and then I created a JPanel and added the JEditorPane to it and then added that JPanel to the JFrame.

    import java.awt.BorderLayout;
    import java.awt.Dimension;
    
    import javax.swing.JEditorPane;
    import javax.swing.JFrame;
    import javax.swing.JPanel;
    
    public class JEditorPaneTest extends JFrame {
        
        public static void main(String[] args) {
            final JFrame mainFrame = new JFrame("test");
            mainFrame.setSize(500,500);
                
            final JEditorPane field = new JEditorPane();
            JPanel panel = new JPanel();
            field.setContentType("text/html");
            field.setText(htmlize("Lorem ipsum dolor sit amet, consectetur adipiscing elit. Suspendisse accumsan magna vel libero bibendum, quis hendrerit nisi rutrum. Cras placerat erat eget dictum ornare. Sed eget nisl quis nibh vehicula mollis. Vestibulum non iaculis erat, quis pulvinar magna. Suspendisse ac rhoncus purus. Quisque finibus, dolor varius tincidunt aliquet, mauris felis condimentum neque, at viverra felis nulla at justo. Duis ut dui velit. Integer vitae mollis leo. Cras quis urna odio. Suspendisse tempus, urna sed maximus fringilla, ante velit finibus massa, id commodo libero quam non ipsum. Sed id augue vitae sapien sagittis imperdiet in eget nibh. Nam semper posuere nisl, dictum efficitur ipsum aliquet ac. Phasellus eros massa, fringilla et neque maximus, pretium tempor magna."));
            
            field.setSize(200,40);
            field.setPreferredSize(new Dimension(200, field.getPreferredSize().height));
            panel.add(field);
                    
            mainFrame.getContentPane().add(panel,BorderLayout.CENTER);
            mainFrame.setVisible(true);
            System.out.println(field.getPreferredSize().width+" , "+field.getPreferredSize().height);
        }
        
        public static String htmlize(String pMessage) {
            return "<html><body>" + pMessage + "</body></html>";
        }
    }
    

    This is how it looks.

    JEditorPane