Java - Provide an example where two applets communicate each other

Applet Communication

java.applet.AppletContext class facilitates the communication between applets.

Example

We have two applets ButtonApplet and ColorApplet, when a button of ButtonApplet is clicked, the background color of the ColorApplet changes. To create two objects, we write two times the tag within the same tag.

myapplet.html

<html>
<body>
<applet code="ButtonApplet.class" width="150" height="150" name="btn">
</applet>

<applet code="ColorApplet.class" width="150" height="150" name="clr">
</applet>
</body>
</html>

import java.applet.*;
import java.awt.*;
import java.awt.event.*;
public class ButtonApplet extends Applet implements ActionListener
{
    Button btnColor;

    public void init()
    {
        btnColor=new Button("Click");
        btnColor.setBounds(50,50,60,50);
        add(btnColor);
        btnColor.addActionListener(this);
    }

    public void actionPerformed(ActionEvent e)
    {
        AppletContext ctx=getAppletContext();
        Applet a=ctx.getApplet("clr");
        a.setBackground(Color.red);
    }
}
Java - Explain how to play sound in an applet
Explain how to play sound in an applet import java.applet.*;.....
Java - Explain how to play audio in a stand alone application
How to play audio in a stand alone application...
Java - What is the difference between an Applet and an Application?
Difference between an Applet and an Application - An applet runs with the control of a browser, where as an application runs standalone...
Post your comment