0

In my Java web browser, the javascript alert() is not working how to make it active?

Example:

package www;
import java.awt.BorderLayout;
import java.awt.Dimension;
import javax.swing.JEditorPane;
import javax.swing.JFrame;
import javax.swing.JScrollPane;
import javax.swing.SwingUtilities;
import javax.swing.text.Document;
import javax.swing.text.html.HTMLEditorKit;
import javax.swing.text.html.StyleSheet;

public class HtmlEditorKitTest {
  public static void main(String[] args) {
    new HtmlEditorKitTest();
  }

  public HtmlEditorKitTest() {
    SwingUtilities.invokeLater(new Runnable() {
      public void run() {
        JEditorPane jEditorPane = new JEditorPane();
        jEditorPane.setEditable(false);
        JScrollPane scrollPane = new JScrollPane(jEditorPane);
        HTMLEditorKit kit = new HTMLEditorKit();
        jEditorPane.setEditorKit(kit);
        StyleSheet styleSheet = kit.getStyleSheet();
        styleSheet.addRule("body {color:#000; font-family:times; margin: 4px; }");
        styleSheet.addRule("h1 {color: blue;}");
        styleSheet.addRule("h2 {color: #ff0000;}");
        styleSheet.addRule("pre {font : 10px monaco; color : black; background-color : #fafafa; }");
        String htmlString = "<html>\n"
                          + "<body>\n"
                          + "<script>alert('Show Javascript works or not');</script>\n"
                          + "<h1>Welcome!</h1>\n"
                          + "<h2>H2 header</h2>\n"
                          + "<p>Text description</p>\n"
                          + "<p><a href=\"http://www.google.com/can/whynot?Oracle?/\">SiteOpen</a></p>\n"
                          + "</body>\n";

        Document doc = kit.createDefaultDocument();
        jEditorPane.setDocument(doc);
        jEditorPane.setText(htmlString);
        JFrame j = new JFrame("WebBrowser in Java");
        j.getContentPane().add(scrollPane, BorderLayout.CENTER);
        j.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        j.setSize(new Dimension(300,200));
        j.setLocationRelativeTo(null);
        j.setVisible(true);
      }
    });
  }
}
3

1 Answer 1

3

Calling something a web browser doesn't make it a web browser.

From the HTMLEditorKit API:

The Swing JEditorPane text component supports different kinds of content via a plug-in mechanism called an EditorKit. Because HTML is a very popular format of content, some support is provided by default. The default support is provided by this class, which supports HTML version 3.2 (with some extensions), and is migrating toward version 4.0. The <applet> tag is not supported, but some support is provided for the <object> tag.

1
  • 1
    After making a functional applet context and environment, I went to add support for the applet element in the editor kit of a JEditorPane (for use in Appleteer). I achieved it, but it was the toughest part of the entire process. Commented Dec 29, 2011 at 10:36