Wrapping HTML text in a JEditorPane inside a JScrollPane

mjomble picture mjomble · Mar 2, 2011 · Viewed 12.6k times · Source

In an application, I'm using uneditable JEditorPanes as a sort of a generic UI widget that can display somewhat complex content (HTML will do the trick), wrap text lines and catch mouse clicks. Not sure if JEditorPane is a good choice for this, so feel free to suggest alternatives.

The following sample code works fairly well:

import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;

import javax.swing.JEditorPane;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.ScrollPaneConstants;

public class Main {

  private static JPanel createPanel() {
    JPanel panel = new JPanel();
    panel.setLayout(new GridBagLayout());

    for (int i = 0; i < 3; i++) {
      JEditorPane editorPane = new JEditorPane();
      editorPane.setEditable(false);
      editorPane.setContentType("text/html");

      String text =
        "This is <b>item #" + i + "</b>." +
        " It's got text on it that should be wrapped."
      ;

      editorPane.setText(text);

      GridBagConstraints constraints = new GridBagConstraints();
      constraints.gridx = 0;
      constraints.gridy = i;
      constraints.fill = GridBagConstraints.HORIZONTAL;
      constraints.weightx = 1.0;
      constraints.insets.bottom = 5;

      panel.add(editorPane, constraints);
    }

    return panel;
  }


  public static void main(String[] args) {
    JPanel panel = createPanel();

    JFrame frame = new JFrame();
    frame.setSize(200, 200);
    frame.setLocation(200, 200);

    // Change this to switch between examples
    boolean useScrollPane = false;

    if (useScrollPane) {
      JScrollPane scrollPane = new JScrollPane();
      scrollPane.setHorizontalScrollBarPolicy(ScrollPaneConstants.HORIZONTAL_SCROLLBAR_NEVER);
      scrollPane.setViewportView(panel);
      frame.add(scrollPane);
    }
    else {
      frame.add(panel);
    }

    frame.setVisible(true);
  }

}

and produces the following:

However, I may have a large number of these and could use a vertical scrollbar.

So I put the whole thing in a JScrollPane (change the useScrollPane variable to true in the sample code to see this version).

This gives me a vertical scrollbar if I shrink the window height, but the problem is that now the text is no longer wrapped:

So the question is: how can I get both the text wrapping and the vertical scrollbar?

As you can see, I disabled the horizontal scrollbar, but it didn't help much.

PS. I don't have much experience with Swing, so if you see some beginner WTFs in this code, please point them out :)

Answer

camickr picture camickr · Mar 3, 2011
// JPanel panel = new JPanel();
ScrollablePanel panel = new ScrollablePanel();
panel.setScrollableWidth( ScrollablePanel.ScrollableSizeHint.FIT );

See Scrollable Panel for the class and an explanation on how the class works.