Doubt in java programming...?

Morningdot Hablu

Morningdot Hablu

@morningdot-6Xuj4M Oct 23, 2024
hello friends,
check these codes..
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(40 );
            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 goes here...
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(40,50);
        add(t);
    }
    public void append(String msg)
    {
        t.setText(msg);
    }
}
here goes the html codes...
<HTML><HEAD></HEAD><BODY>
<APPLET CODE="pp.class"   
        HEIGHT=400 WIDTH=400>
</APPLET>
<APPLET CODE="dc.class"  
        HEIGHT=500 WIDTH=500>
</APPLET>
</BODY></HEAD>
Don't know why it doesn't works.
Can anyone tell me where i have done mistake here....?

Replies

Welcome, guest

Join CrazyEngineers to reply, ask questions, and participate in conversations.

CrazyEngineers powered by Jatra Community Platform

  • sookie

    sookie

    @sookie-T06sFW Jan 4, 2011

    @mohit Can you please show the error logs ? What error you are getting?
  • Morningdot Hablu

    Morningdot Hablu

    @morningdot-6Xuj4M Jan 5, 2011

    No error sookie...it will compiled fine but communication between applets doesn't take place...check this.....#-Link-Snipped-#
  • sookie

    sookie

    @sookie-T06sFW Jan 6, 2011

    Hi Mohit,

    I checked your code and found that in pp.java at line # 20
    dc a2 =(dc)getAppletContext().getApplet("a2");
    You are trying to call an applet with name as "a2" as per the AppletContext.getApplet method specifications but I checked in your HTML page, there is no name specified. You are supposed to specify the name of the "dc" applet as "a2" in the HTML code. So now your new HTML code should be
    <HTML><HEAD></HEAD><BODY>
    <APPLET CODE="pp.class"   
            HEIGHT=400 WIDTH=400>
    </APPLET>
    <APPLET CODE="dc.class" name="a2" 
            HEIGHT=500 WIDTH=500>
    </APPLET>
    </BODY></HEAD>
    
    Now run, it will work. Let me know if any questions.

    -Sookie