devxlogo

How Can Java Applet Display HTML?

How Can Java Applet Display HTML?

Question:
I believe there is a JavaScript function: ‘ShowDocument(URL,targetFrame)’ Is there a way to replicate this in Java?

I realize that in the future the solution may materialize when a relationship between Java and JavaScript develops. I presume Java would then have the ability to access JavaScript variables and functions.

Answer:
Here’s the standard technique for displaying a Web page and reading itsHTML:

import java.awt.*;import java.applet.*;import java.net.*;import java.io.*;public class Browse extends Applet {   private TextField path;   // private ErrorFrame f = new ErrorFrame();   URL source;   TextArea display = new TextArea(8, 40); // 8 lines x 40 chars   String next;   AppletContext context;   public void init() {      setBackground(Color.pink);      add(path = new TextField(“”, 30));      Panel p = new Panel();      p.add(new Button(“SHOW HTML”));      p.add(new Button(“HIDE HTML”));      add(p);      add(display);      context = getAppletContext();   }   public boolean action(Event e, Object arg) {            if (e.target == path) {         try {            String file = path.getText();            source = new URL(getCodeBase(), file);            context.showDocument(source, “_self”);         }         catch(MalformedURLException mue) {         //   f.error(mue);         }         return true;      }      else if (arg.equals(“SHOW HTML”)) {         try {            DataInputStream in = new DataInputStream(source.openStream());            // erase display            display.setText(“”);            while(null != (next = in.readLine()))               display.appendText(next + “/n”);            in.close();            return true;         }         catch(IOException ioe) {           // f.error(ioe);         }      }      else if (arg.equals(“HIDE HTML”)) {         display.setText(“”);         return true;      }      return false;   }} 

See also  How AI Art Generators Can Save Time and Money for Startups
devxblackblue

About Our Editorial Process

At DevX, we’re dedicated to tech entrepreneurship. Our team closely follows industry shifts, new products, AI breakthroughs, technology trends, and funding announcements. Articles undergo thorough editing to ensure accuracy and clarity, reflecting DevX’s style and supporting entrepreneurs in the tech sphere.

See our full editorial policy.

About Our Journalist