27.04.2013 Views

330 Java Tips.pdf - FTP Server

330 Java Tips.pdf - FTP Server

330 Java Tips.pdf - FTP Server

SHOW MORE
SHOW LESS

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

Applets<br />

Answer: You will occasionally need to allow two or more applets on a Web page to communicate with<br />

each other. Because the applets all run within the same <strong>Java</strong> context-that is, they are all in the same<br />

virtual machine together-applets can invoke each other's methods. The AppletContext class has<br />

methods for locating another applet by name, or retrieving all the applets in the current runtime<br />

environment<br />

-----------------------------------------------------------------<br />

import java.applet.*;<br />

import java.awt.*;<br />

import java.util.*;<br />

// This applet demonstrates the use of the getApplets method to<br />

// get an enumeration of the current applets.<br />

public class ListApplets extends Applet {<br />

public void init() {<br />

// Get an enumeration all the applets in the runtime environment<br />

Enumeration e = getAppletContext().getApplets();<br />

// Create a scrolling list for the applet names<br />

List appList = new List();<br />

while (e.hasMoreElements()) {<br />

// Get the next applet<br />

Applet app = (Applet) e.nextElement();<br />

// Store the name of the applet's class in the scrolling list<br />

appList.addItem(app.getClass().getName());<br />

}<br />

add(appList);<br />

}<br />

}<br />

I hope that did it!<br />

by 11037803<br />

Here are some useful links on applet to applet communication. I don't know if they will solve your<br />

problem but these are a variety of good approaches for this type of issue.<br />

http://www.javaworld.com/javaworld/javatips/jw-javatip101.html<br />

http://www.twf.ro/calculatoare/Tricks<strong>Java</strong>ProgramGurus/ch1.htm<br />

http://www.galasoft-lb.ch/myjava/CommTest/backup00/<br />

http://www.rgagnon.com/javadetails/java-0181.html<br />

http://www.2nu.com/Doug/FAQs/InterframeIAC.html<br />

by Mickey Segal<br />

Q: I would like to ask if there 's anyway that I can use the same program run as an applet or<br />

application?<br />

Answer: You would have to provide at least a main() for the application part, and init(), start(), stop(),<br />

destroy() for the applet part of your program. Your class could simply display the applet within a<br />

Frame.<br />

Example:<br />

class Foo extends Frame {<br />

public Foo(String title){<br />

//...<br />

file:///F|/350_t/350_tips/applets.htm (7 of 10) [2002-02-27 21:17:49]

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!