Sam Palmer
Sam Palmer

Reputation: 1725

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

Upvotes: 2

Views: 7427

Answers (2)

Sorro
Sorro

Reputation: 101

I used the method setPreferredSize instead of setSize, like in the code below:

        JEditorPane jEditorPane = new JEditorPane();
        jEditorPane.setEditable(false);
        jEditorPane.setContentType("text/html");
        jEditorPane.setText(toolTipText);
        jEditorPane.setPreferredSize(new Dimension(800, 600));

This should work with any container - in my case, I used it with a scroll pane.

HTH!

Upvotes: 1

laher
laher

Reputation: 9110

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);
    }
}

Upvotes: 3

Related Questions