0

Please check these code samples:

import java.awt.*;
import java.applet.*;
import java.awt.event.*;

public class pp extends Applet implements ActionListener
{
    TextField t;
    Button    b;

    public void init()     
    {
        setLayout(new FlowLayout());
        t= new TextField(10 );
        b = new Button("Send");
        b.addActionListener(this);
        add(t);
        add(b);
    }

    public void actionPerformed(ActionEvent e) 
    {
        String str=t.getText();
        dc a2 =(dc)getAppletContext().getApplet("a2");
           if ( a2 != null ) 
               {
                  a2.append(str);
               }
           else 
           {
              System.out.println("Applet not found?");
           }
     }
}

and the 2nd applet code:

import java.applet.*;
import java.awt.*;
import java.awt.event.*;
/*<Applet code="dc" height=400 width=400></Applet>*/
public class dc extends Applet
{
    TextArea t;

    public void init()
    {
        setLayout(new FlowLayout());
        t=new TextArea(5,40);
        add(t);
    }

    public void append(String msg)
    {
        t.setText(msg);
    }
}

and here is the HTML code:

<HTML><HEAD></HEAD><BODY>
<APPLET CODE="pp.class"   
    HEIGHT=200 WIDTH=150>
</APPLET>
<APPLET CODE="dc.class"  
    HEIGHT=200 WIDTH=400>
</APPLET>
</BODY></HEAD>

Don't know why it doesn't work. This program is writeen for communication between the two applets in the same page. Can anyone tell me what is wrong here?

ismail
  • 46,010
  • 9
  • 86
  • 95
user528050
  • 157
  • 2
  • 4
  • 21
  • Please format your source code properly. – Oswald Dec 29 '10 at 16:12
  • What do you get on the Java console? Does it say "Applet not found?" – Cameron Skinner Dec 29 '10 at 16:14
  • 1
    If you want help, please read the "how to ask a question" section before posting on here. http://stackoverflow.com/questions/how-to-ask . Your "Doubt in java programming...!!" title tells us nothing about what you want to know. Also, it helps to format your code so it's readable. – 08Hawkeye Dec 29 '10 at 16:15

2 Answers2

1

See Inter applet communication

This looks very similar to what you are doing.

Community
  • 1
  • 1
Cameron Skinner
  • 51,692
  • 2
  • 65
  • 86
1

First applet:

import java.awt.*;
import java.applet.*;
public class ONE extends Applet
{
    TextArea ta;
    public void init()
    {
        ta=new TextArea(" ");
        add(ta);
    }
    public void putText(String s)
    {
        ta.appendText(s+"\n");
    }
}

Second applet:

import java.io.*;
import java.applet.*;
import java.awt.event.*;
import java.awt.*;


public class TWO extends Applet implements ActionListener
{
    TextField tf;
    Applet r;
    Button b;
    public void init()
    {
        tf=new TextField(20);
        add(tf);
        b=new Button("SUMBIT");
        add(b);
        b.addActionListener(this);
    }
    public void actionPerformed(ActionEvent e)
    {   r=null;
        r= getAppletContext().getApplet("ONE");
        if (r!=null)
        {
            if(e.getSource()==b)
            {
                ONE ma= (ONE) r;
                ma.putText(tf.getText());
                tf.setText("");
            }
        }       
    }
}

Use the above code in action performed.

HTML:

<html>
<body>
<applet code="TWO" width = 150 height=150 name=TWO>
</applet>
<br></br>
<br></br>
<br></br>
<applet code="ONE" width = 200 height=200 name=ONE>
</applet>
</body>
</html>

Run it using appletviewer.

DarthJDG
  • 16,511
  • 11
  • 49
  • 56
SAHAR
  • 11
  • 2