The Artima Developer Community
Sponsored Link

Java Answers Forum
sharing information between applets - help!

2 replies on 1 page. Most recent reply: Aug 30, 2002 8:06 AM by Kishori Sharan

Welcome Guest
  Sign In

Go back to the topic listing  Back to Topic List Click to reply to this topic  Reply to this Topic Click to search messages in this forum  Search Forum Click for a threaded view of the topic  Threaded View   
Previous Topic   Next Topic
Flat View: This topic has 2 replies on 1 page
Julio C. Fonseca C.

Posts: 18
Nickname: jfonseca
Registered: Mar, 2002

sharing information between applets - help! Posted: Aug 23, 2002 10:27 PM
Reply to this message Reply
Advertisement
Hi,

This is me again and I wanna share information between applets via data defined in a third class,.

This is the scenario,
I'm using two applets and a java class, the two applets (applet1 and applet2) wants to share an int variable "shareVariable" defined in a third class called "SharingClass"

I do know that it is quite straightforward and easy If I just use information defined between applet1 and applet2 WITHOUT using a third class.

But , What happens when I use a third class?, I mean what happens with the the context of the variable, why it's created again if it's supposed to be static and accesed without any object.

I will post this three classes,

applet1, it sets the value and recover it


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

public class Applet1 extends Applet
{
public void init ()
{
;
}

public void paint (Graphics g)
{
//I would expect the value 100, and this what it shows
g.drawString("Before setting the value : " + SharingClass.getValue(), 10, 10);

SharingClass.setValue(500);

//The values is already changed and it shows 500
g.drawString("Before setting the value : " + SharingClass.getValue(), 50, 50);
}
}

the sharing class, the class which contains the static value

public class SharingClass
{
private static int shareVariable = 100;

public static void setValue (int value)
{
shareVariable = value;
}

public static int getValue ()
{
return shareVariable;
}
}


applet2 I would like recover the valued set by applet1, but it doesn't

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

public class Applet2 extends Applet
{
public void init ()
{
;
}

public void paint (Graphics g)
{
//I would expect, or it least I would like to get 500, but it doesn't happens, it shows 100 again
//it seems it use a different variable
g.drawString("Before setting the value : " + SharingClass.getValue(), 10, 10);
}
}

I would like to maintain this architecture, because I'm really working on a major project and I have face this problem,

How can it be solved using this three same classes.

Thanks in advance,

Julio


Sebastian Rose

Posts: 2
Nickname: bastianr
Registered: Aug, 2002

Re: sharing information between applets - help! Posted: Aug 30, 2002 3:30 AM
Reply to this message Reply
High. Hope you like it. This is a primitive EventListener ;-)
There is a clas javax.swing.event.ChangeListener too. But it
that doesn't work with older JRE's.

1.) An interface, that all classes must implement, if they want
to recieve data from SharingClass.
You could change it, so a reciever can send data to.
It might be importend, to synchronize SharingClass.setValue() then.

public interface Reciever {
void recieve(int value);
}


2.) The first Applet is the sender.

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

public class applet1 extends Applet implements Runnable {

private int number;

public void init () { number = 0; }
public void start() {
SharingClass.sender = new Thread(this);
SharingClass.sender.start();
}
public void run() {
while(SharingClass.sender == Thread.currentThread()) {
try {
Thread.sleep(200);
}
catch(InterruptedException ire) {}
number++;
repaint();
SharingClass.setValue(number);
}
}
public void stop() { SharingClass.sender = null; }
public void paint (Graphics g) {
g.drawString("Setting the value to: " + number, 50, 50);
}
}


3.) The second Applet is a Reciever.
import java.applet.*;
import java.awt.*;


public class applet2 extends Applet implements Reciever {

private Thread runner;
private int number;

public void init () { SharingClass.addReciever(this); }
public void recieve(int value) {
number = value;
repaint();
}
public void paint (Graphics g) {
g.drawString("Recieved " + number, 50, 50);
}
}


4.) The changed SharingClass
import java.util.Vector;
import java.util.Enumeration;

public class SharingClass {

private static Vector recievers = new Vector(10);
private static int shareVariable = 100;
protected static Thread sender;


public static void setValue (int value) {
shareVariable = value;
Enumeration enum = recievers.elements();
while(enum.hasMoreElements()) {
((Reciever)enum.nextElement()).recieve(shareVariable);
}
}
// Register Recievers.
public static void addReciever(Reciever r) {
recievers.addElement(r);
}
}

Kishori Sharan

Posts: 211
Nickname: kishori
Registered: Feb, 2002

Re: sharing information between applets - help! Posted: Aug 30, 2002 8:06 AM
Reply to this message Reply
It works fine for me. In applet 1 it showed 100 and 500 and in applet 2 it showed 500. Maybe you are including applet2 first in your html page and then applet1. Try including applet1 and then applet2. I have used IE6.0 and JRE1.4

Thanks
Kishori

Flat View: This topic has 2 replies on 1 page
Topic: string and string buffer?? Previous Topic   Next Topic Topic: chat room

Sponsored Links



Google
  Web Artima.com   

Copyright © 1996-2019 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use