2

This is very similar to my last question, but doing it in a JPanel I am trying to display a JEditorPane inside a JPanel (fixed size) which then displays a string of HTML text. (This is so I can use a cardlayout and switch panels with different displays). I can get everything displaying again, 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! It works when the EditorPane is directly inside a JFrame but not JPanel.

    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

Heisenbug
  • 37,414
  • 27
  • 126
  • 181
Sam Palmer
  • 1,557
  • 1
  • 23
  • 42
  • take a look at http://stackoverflow.com/questions/5462798/java-se-jeditorpane-not-wrapping-text-on-resize-when-using-miglayout – Mead3000 Oct 27 '11 at 09:49

2 Answers2

6
..."Unfortunately when I display this string it is too long and doesn't wrap to new line!"

Set the width by using a style in the body element, as in this example.

Community
  • 1
  • 1
Andrew Thompson
  • 163,965
  • 36
  • 203
  • 405
3

Well, you shold never use setXXXSize methods from your application. See this thread.

Just a few considerations:

  1. It's up to the LayoutManager positioning and establish the right size of a component, so you shouldn't specify it explicitely
  2. Not all LayoutManagers take care of preferredSize values of the components.
  3. Even if the LayoutManager takes care of preferred size, it's wrong to explicitely use those methods
  4. If you want to explicitely use a size, do it only with the parent frame containing all components.
  5. If a LayoutManager doesn't lay components in the right way, use a different LayoutManager or implement your own.

A note:

I know that in certain situations use setXXXmethods seems to work fine. I did that for quite a long time before understanding how much it's wrong. Try to avoid that or, soon or late, you will have a lot of code to refactor.

Community
  • 1
  • 1
Heisenbug
  • 37,414
  • 27
  • 126
  • 181