java - Load javascript within JFrame or JPanel - Stack Overflow

I would like to load a the below javascript code to a JPanel or a JFrame. Is it possible? <scr

I would like to load a the below javascript code to a JPanel or a JFrame. Is it possible?

<script language="javascript" type="text/javascript" charset="utf-8">
//javascript code
</script>

I would like to load a the below javascript code to a JPanel or a JFrame. Is it possible?

<script language="javascript" type="text/javascript" charset="utf-8">
//javascript code
</script>
Share Improve this question edited Mar 3, 2012 at 18:26 Eng.Fouad 118k74 gold badges324 silver badges427 bronze badges asked Mar 3, 2012 at 17:59 Jessy JamesonJessy Jameson 2072 gold badges6 silver badges15 bronze badges 1
  • The language attribute was deprecated in HTML 4. See the HTML 4 documentation. – Andrew Thompson Commented Mar 3, 2012 at 18:13
Add a ment  | 

3 Answers 3

Reset to default 3

You can use third-party libraries: such as HtmlUnit

It has fairly good JavaScript support (which is constantly improving) and is able to work even with quite plex AJAX libraries, simulating either Firefox or Internet Explorer depending on the configuration you want to use.

Example:

import .gargoylesoftware.htmlunit.BrowserVersion;
import .gargoylesoftware.htmlunit.WebClient;
import .gargoylesoftware.htmlunit.html.HtmlPage;

public class JavaScriptProgram
{
    private WebClient webClient;
    private HtmlPage currentPage;

    public JavaScriptProgram()
    {
        webClient = new WebClient(BrowserVersion.INTERNET_EXPLORER_8);
        webClient.setThrowExceptionOnScriptError(false);
        webClient.setJavaScriptEnabled(true);
    }

    public void visitPage(String url)
    {
        try
        {
            currentPage = (HtmlPage) webClient.getPage(url);
        }
        catch(Exception e)
        {
            // Cannot get the page
        }
    }

    public void executeJavaScript(String code)
    {
        currentPage.executeJavaScript(code);
    }

    public static void main(String[] args)
    {
        JavaScriptProgram p = new JavaScriptProgram();
        p.visitPage("http://www.stackoverflow.");
        p.executeJavaScript("document.write('Hello World!');");
    }
}

If you want browser capability within your Swing app, you could check Lobo, it supports Javascript too.

Swing ponents doesn't support Javascript codes natively. If you really need integrate Javascript and Java, you can try Rhino. But remember: it's javascript language only, you cannot use browser's API.

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1744698629a4588645.html

相关推荐

  • java - Load javascript within JFrame or JPanel - Stack Overflow

    I would like to load a the below javascript code to a JPanel or a JFrame. Is it possible? <scr

    20小时前
    30

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信