i have this code to render a locally saved html file onto a jframe. i have the following problems with how the html was rendered:

  1. the texts of the 'webpage' do not wrap;
  2. in order to solve my problem in 1. above, i added a JScrollPane, problem is, the scrollbars do not appear.

any help is appreciated. thanks.

i have the code below:
(main method -TryJEditor.java)

import javax.swing.JFrame;

public class TryJEditor {

    public static void main(String[] args) {

        TryHtml itry = new TryHtml();
        itry.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        itry.setSize(500, 300);
        itry.setVisible(true);  
    }
}

(and the class - TryHtml.java)

import java.awt.*;
import java.io.File;
import java.io.IOException;
import java.net.MalformedURLException;
import javax.swing.*;

public class TryHtml extends JFrame {

    private JEditorPane HtmlPane;
    private JPanel pane;
    private JScrollPane scroller;
    private String url = "file:///D:\\Java Exercise Programs\\TryJEditor\\myhtml.htm";

    public TryHtml() {
        super("title");
        FlowLayout fl = new FlowLayout(FlowLayout.LEFT);
        setLayout(fl);

        pane = new JPanel();
        try {
            pane = new JPanel();
            HtmlPane= new JEditorPane(url);         
            HtmlPane.setContentType("text/html");
            HtmlPane.setEditable(false);
            HtmlPane.setSize(500, 250); 
            scroller = new JScrollPane(HtmlPane, ScrollPaneConstants.VERTICAL_SCROLLBAR_AS_NEEDED, ScrollPaneConstants.HORIZONTAL_SCROLLBAR_AS_NEEDED);

        } catch (IOException e) {
            e.printStackTrace();
        }

        pane.add(scroller);
        add(pane);
        pack();     
    }
}

disregard.. i found some ideas from the internet.
i just added this new line after creating the JScrollPane.

scroller.setPreferredSize(new Dimension(int, int));
This article has been dead for over six months. Start a new discussion instead.