title: Wrapping HTML text in a JEditorPane inside a JScrollPane
body: <p>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.</p>
  
  <p>The following sample code works fairly well:</p>
  
  <pre><code>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 &lt; 3; i++) {
        JEditorPane editorPane = new JEditorPane();
        editorPane.setEditable(false);
        editorPane.setContentType("text/html");
  
        String text =
          "This is &lt;b&gt;item #" + i + "&lt;/b&gt;." +
          " 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);
    }
  
  }
  </code></pre>
  
  <p>and produces the following:</p>
  
  <p><img src="https://dl.dropbox.com/u/3110371/stackoverflow/jeditorpane_wrap_1.png" /></p>
  
  <p>However, I may have a large number of these and could use a vertical scrollbar.</p>
  
  <p>So I put the whole thing in a JScrollPane (change the <code>useScrollPane</code> variable to <code>true</code> in the sample code to see this version).</p>
  
  <p>This gives me a vertical scrollbar if I shrink the window height, but the problem is that now the text is no longer wrapped:</p>
  
  <p><img src="https://dl.dropbox.com/u/3110371/stackoverflow/jeditorpane_wrap_2.png" /></p>
  
  <p>So the question is: how can I get both the text wrapping and the vertical scrollbar?</p>
  
  <p>As you can see, I disabled the horizontal scrollbar, but it didn't help much.</p>
  
  <p>PS. I don't have much experience with Swing, so if you see some beginner WTFs in this code, please point them out :)</p>