dcsimg
Login | Register   
LinkedIn
Google+
Twitter
RSS Feed
Download our iPhone app
TODAY'S HEADLINES  |   ARTICLE ARCHIVE  |   FORUMS  |   TIP BANK
Browse DevX
Sign up for e-mail newsletters from DevX

By submitting your information, you agree that devx.com may send you DevX offers via email, phone and text message, as well as email offers about other products and services that DevX believes may be of interest to you. DevX will process your information in accordance with the Quinstreet Privacy Policy.


Tip of the Day
Language: Applets
Expertise: Beginner
Mar 21, 1997

WEBINAR:

On-Demand

Building the Right Environment to Support AI, Machine Learning and Deep Learning


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 its HTML:

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;
   }


} 
DevX Pro
 
Comment and Contribute

 

 

 

 

 


(Maximum characters: 1200). You have 1200 characters left.

 

 

Sitemap
Thanks for your registration, follow us on our social networks to keep up-to-date