开发者

Load html frameset code from memory to Java SWT browser

I have a simple html editor written with SWT and it's Brow开发者_运维问答ser control. I would like to know if there is a way to load the code for html frame pages from memory, without saving the html file on the hdd?


The code which are you looking for is one of the SWT Browser widget snippets. See render HTML from memory.

import org.eclipse.swt.*;
import org.eclipse.swt.browser.*;
import org.eclipse.swt.layout.*;
import org.eclipse.swt.widgets.*;

public class Snippet136 {
    public static void main(String [] args) {
        String html = "<HTML><HEAD><TITLE>HTML Test</TITLE></HEAD><BODY>";
        for (int i = 0; i < 100; i++) html += "<P>This is line "+i+"</P>";
        html += "</BODY></HTML>";

        Display display = new Display();
        Shell shell = new Shell(display);
        shell.setLayout(new FillLayout());
        Browser browser;
        try {
            browser = new Browser(shell, SWT.NONE);
        } catch (SWTError e) {
            System.out.println("Could not instantiate Browser: " + e.getMessage());
            display.dispose();
            return;
        }
        browser.setText(html);
        shell.open();
        while (!shell.isDisposed()) {
            if (!display.readAndDispatch())
                display.sleep();
        }
        display.dispose();
    }
}

There are few others snippets which may became handy for you.. Check SWT Browser's widget snippets.


Add one line in your in memory HTML will do the trick.

For example, assume your files page1.html etc. locate in your local computer at C:/myHTML, then you need add this to your in memory HTML string as below,

String html="<HTML><HEAD><TITLE>HTML Test</TITLE>"
            +"<BASE href=\"file:///C:myHTML\\/\" >"
            +"<HEAD>"
            +"<BODY><A src=\"page1.html\">Page1</A></BODY></HTML>"

This works for me on Win 7.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜