Search code examples
javaswingjeditorpane

Fixing the size of JEditorPane Content


I am trying to display a JEditorPane inside a JFrame (fixed size) which then displays a string of HTML text. I can get everything displaying, though it seems that the text inside the HTML string I pass into my EditorPane doesn't cut off and wrap to a new line. i.e it just extends off the screen. It seems that the setSize method has no bearing on the size of the Pane?

I am making this app for different size screen so it is important that the text wraps to new lines and fits the screen size rather than run off!

    JEditorPane pane = new JEditorPane();
    pane.setEditable(false);
    HTMLDocument htmlDoc = new HTMLDocument () ; 
    HTMLEditorKit editorKit = new HTMLEditorKit () ;
    pane.setEditorKit (editorKit) ; 
    pane.setSize(size);
    pane.setMinimumSize(size); 
    pane.setMaximumSize(size);
    pane.setOpaque(true);
    pane.setText("<b><font face=\"Arial\" size=\"50\" align=\"center\" > Unfortunately when I display this string it is too long and doesn't wrap to new line!</font></b>");
    bg.add(pane, BorderLayout.CENTER);

Many thanks Sam


Solution

  • Works for me ... perhaps you initialized it differently?

    import java.awt.BorderLayout;
    import java.awt.Container;
    import java.awt.Dimension;
    
    import javax.swing.JEditorPane;
    import javax.swing.JFrame;
    import javax.swing.text.html.HTMLDocument;
    import javax.swing.text.html.HTMLEditorKit;
    
    public class JEditorPaneTest extends JFrame {
        public static void main(String[] args) {
            JEditorPaneTest t= new JEditorPaneTest();
            t.setSize(500,500);
            Container bg = t.getContentPane();
            t.createJEditorPane(bg, bg.getSize());
            t.setVisible(true);
        }
    
        public void createJEditorPane(Container bg, Dimension size) {
            JEditorPane pane = new JEditorPane();
            pane.setEditable(false);
            HTMLDocument htmlDoc = new HTMLDocument();
            HTMLEditorKit editorKit = new HTMLEditorKit();
            pane.setEditorKit(editorKit);
            pane.setSize(size);
            pane.setMinimumSize(size);
            pane.setMaximumSize(size);
            pane.setOpaque(true);
            pane.setText("<b><font face=\"Arial\" size=\"50\" align=\"center\" > Unfortunately when I display this string it is too long and doesn't wrap to new line!</font></b>");
            bg.add(pane, BorderLayout.CENTER);
        }
    }