Link to home
Start Free TrialLog in
Avatar of kitbishop
kitbishop

asked on

Browser Document Output from Java Applet

I'd be most greatful if anyone could help me with the following problem.

In JavaScript, I can output to the current browser document using :

      document.writeln(.....)

I would really like to be able to do the same thing from a Java Applet but
have not found a way to do so.  Does anyone have any ideas on how it can be
done, or point me at any useful web page for reference.

I have managed to produce the effect I want by using JavaScript to call an
applet method that returns the text I want to write, but I really would
like to bypass this extra step.

Thanks in anticipation of helpful replies.

Best regards,

Kit Bishop,
Bishop Consulting
E-mail cbishop@voyager.co.nz
Avatar of jpk041897
jpk041897

For an applet to display the contents of a given url you can use one of two methods:

void showDocument(URL url)

Shows the specified document in the current context.


or

void showDocument(URL url, String target)

Requests that the browser show the specified document in a particular location—either a browser window or a Netscape frame.

Options for target:
Value Document Displayed in:

"_self" Current Frame
"_parent" Parent Frame
"_top" Top-most Frame
"_blank" In a new and unnamed browser window
"aNameofYourChoice" Creates a new browser window with the specified name.  You may later display other documents in this window by using the same name as the target.


As an example, you could use:

...
URL myURL;

try{
   URL myUrl = new URL("http://www.netscape.com/index.html");
} catch {MalfomredURLException e){
// Whatever error procesing you want here
}
getAppletContext.showDocument(myURL);

...


Important Note:

Due to bugs in diffrent browser, and browser version, implementations, the only two target values that you can be sure will run as documented on all Java enabled browsers are "_self" and "_parent".
Avatar of kitbishop

ASKER

While appreciating jpk's answer, the material presented is familiar to me and does not solve my problem.

In JavaScript, I can generate any output I want to display in the browser.  For example, the JavaScript :

   document.open();
   document.writeln("<html><body>Hi there</body></html>");
   document.close();

will display in the browser as a new page with just the words :

   Hi there

I would like to be able to do the same direct from the applet.

At present, my solution is something like :

Applet method :

    public String getit()
    {
        return "....."
    }

and JavaScript code :

    document.open();
    document.writeln(document.appletname.getit());
    document.close();

I really would like to be able to do this without having to have the embedded JavaScript.

I.E. I want an applet equivalent to the JavaScript document.writeln

I suggest you grade my answer with and F and see if anyone else has figured out an alternative that can run on all Java enabled browsers.

Unfortunatley for your purposes,applet is only available as an HTML tag, and therfore was not designed to return a value that a Script could use.
Many thanks to jpk's attempts at answering my question, however....

Just by way of further clarification,  what I need to do is not to get access to the applet from the HTML script, but rather to get access to the script variable 'document' from within an applet so I can perform, within the applet, the equivalent to the script command 'document.writeln(...)'
You need to grade the answer with an F or it will continue to appear in the section of questions awaiting evaluation. It will therfore be skipped by most experts:-(.

Please grade it F so that other people will take a look at your qustion. :-)
I really would like to get this problem solved!!!!

kitbishop,

java does not allow to send html directly from an applet to the browser. The standard way would be to use javascript. If you have to do it in java here is a way around it:

Have your applet send the html to a cgi-bin on the server it came from and then use ShowDocument(URL) do retrieve the HTML from the server.

I'm afraid that is only way to do it in java. Note that it is not is not very safe since people could use your cgi-bin to fill up your hard disk.

 hope this helps,

   Philippe


Phillippe,

Sorry for taking so long to get back to you on this, I've not been around for a couple of days.

Clearly, your answer was not what I wanted to hear :-( though I feared it might be the case.

Unfortunately, your suggested solution is not viable for a variety of reasons.

I have failed your answer only in the very vague hope that some one else might have a bright idea.  If within a couple of days I have not received any other satisfactory answer, if you re-answered this with some brief comment, I would give you a non-fail grading as you have genuinely attempted an answer.

Kit Bishop
Here's what I would recommend.  The hierarchy for applets is odd, so check out:
http://java.sun.com/products/jdk/1.1/docs/api/Package-java.applet.html
because what you might be able to do, is use the paint method for the container class.  I've never tried this before, but this(and the call to JS) are the only feasible options.  Try this page, and cruise around the applet part,,also check out the awt section, it might help.  Good Luck
Thanks for all the various suggestions - I've just about given up on a workable solution and think I will have to stick with calling applet methods from JavaScript to do what I want ... shame :-(

You would have thought that the JavaScript objects would be available from within an applet - does anyone have any bright ideas as to how it could be done - sorry I can't pay for it ;-) but it might be an intersting project to encapsulate the JavaScript objecys as classes - if it's feasibale at all.
ASKER CERTIFIED SOLUTION
Avatar of Philippe
Philippe

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Phillipe,

Thanks, I understand what you are saying - I'd basically figured that out myself.  Unfortunately livetalk/hottalk is nio use to me since I want to be able to operate on any common browser that accepts javascript and applets.

Since this is the second time you have attempted to answer this question for me, I'm happy you should get some points for your efforts - thanks.

PS : In doing the work, I have found a rather annoying (but surmountable) difference between MSIE and Netscape, namely that in Netscape, when you do a document.write from javascript, any applet it the same page is stopped.  In MSIE, the applet keeps going until you do a document.close.  Since I wish to do document.write multiple times from data obtained from the applet, this is annoying.  To be compatible, I have to accumulate all the data from the applet in javascript variables before doing any document.write.