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 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.
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;
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