AppletContext within ActionListener..Help!!

Hello,

How can I use AppletContext in actionlistener that is being called by an applet in another class. All I need is on click on that applet, I wanted to open a document or an HTML page. I am using this code, Please see below and help me.

import java.awt.*;

import java.awt.event.*;

import java.lang.*;

import java.util.*;

import java.applet.*;

import java.net.*;

public class Chat1 extends Applet

{

Color color1 = Color.white;

public void init()

{

setLayout(new FlowLayout());

Chat2 button1 = new Chat2("Chat"); // this string "Chat" acts as an applet. On click of this string, I wanted to opea a document

add(button1);

setBackground(color1);

ChatActionListener listener = new ChatActionListener();

button1.addActionListener(listener);

}

}

class ChatActionListener implements ActionListener

{

public ChatActionListener(){ }

public void actionPerformed(ActionEvent ae)

{

if(ae.getActionCommand().equalsIgnoreCase("Chat"))

{

System.out.println("Clicked:" +ae.getActionCommand());

try{

//AppletContext ac;

ac = getAppletContext();

//URL url1 = new URL("YellowPages1.html");

//ac.showDocument(url1);

}

catch(Exception e){e.printStackTrace();}

}

}

}

Thanks for any help.

Uma

[1440 byte] By [reddyumamaheswar] at [2007-9-26 7:25:07]
# 1

Change

ChatActionListener listener = new ChatActionListener();

to

ChatActionListener listener = new ChatActionListener(getAppletContext());

And rewrite the listener like this:

class ChatActionListener implements ActionListener

{

private AppletContext ac;

public ChatActionListener(AppletContext ac) {

this.ac = ac;

}

public void actionPerformed(ActionEvent ae)

{

if(ae.getActionCommand().equalsIgnoreCase("Chat"))

{

System.out.println("Clicked:" +ae.getActionCommand());

try{

URL url1 = new URL("YellowPages1.html");

ac.showDocument(url1);

}

catch(Exception e){e.printStackTrace();}

}

}

}

ashutosh at 2007-7-1 17:18:18 > top of Java-index,Desktop,Core GUI APIs...
# 2

Dear Sir,

Thanks for your reply. Your answer worked and thanks again.A small request,the resultant .html file is being displayed within the same browser window. If suppose I have two frames in an html page and in one frame(frame1) the applet is present and I wanted to display the resultant .html file in another frame(frame2), How can I do this, can you can me out? Do I have to do any HTML coding in the applet or how? Please help me out.

Thanks

Uma

reddyumamaheswar at 2007-7-1 17:18:18 > top of Java-index,Desktop,Core GUI APIs...
# 3
Hi Uma,You can specify the target frame when you call showDocument:ac.showDocument(url1, "frame2"); I don't have html+applet handy to show you, it's been 3 years since I wrote any applets.
ashutosh at 2007-7-1 17:18:18 > top of Java-index,Desktop,Core GUI APIs...