Java Swing How to - Put JEditorPane inside a JScrollPane








Question

We would like to know how to put JEditorPane inside a JScrollPane.

Answer

import java.io.IOException;
/*ww  w  .  ja  v a  2 s.  c o m*/
import javax.swing.JEditorPane;
import javax.swing.JFrame;
import javax.swing.JScrollPane;

public class Main extends JFrame {

  public static void main(String args[]) {
    Main e = new Main();
    e.setVisible(true);
  }

  public Main() {

    JEditorPane web = new JEditorPane();
    web.setEditable(false);

    try {
      web.setPage("http://www.cnn.com");
    } catch (IOException e) {
      web.setContentType("text/html");
      web.setText("<html>Could not load</html>");
    }

    final JScrollPane scrollPane = new JScrollPane(web);
    getContentPane().add(scrollPane);
    this.setBounds(0, 0, 200, 200);

  }

}