Search code examples
javaappletwindowjbutton

Creating only one applet instance and update the rest


Can anyone please suggest on being able to create an applet and share its parameters if another instance of applet is opened in a browser window or standalone applet.

What I am trying to do is to enable a JButton, when the applet is opened. If the button is clicked, the button should disable. The code part is working fine. What is not working fine is, when I open 2 applet windows at the same time (running on the same code), and I click the button, the button disables on the current window; but it doesn't disable on the other.

What I am trying to accomplish is open 2 applet windows, and click the button in one window, and want to see if the button disables in both windows. This is the same applet code, just opened 2 different windows. I have tried the below sample:

public void init() {
    mousePressed();
    click = new JButton("click");
    click.setActionCommand("click");
    if (isFileAvailableForCapture())
        click.setEnabled(true);
    else {
        click.setEnabled(false);
    }
}

void mousePressed(){
    if(click==null)
        System.out.println("This is the value of click" + click);
    click = new JButton("click");
}

How would I achieve this ? Please help !


Solution

  • Applets can only communicate if they are running in the same Virtual Machine instance.

    You might try specifying the SEPARATE_JVM parameter as false, though I am not confident that will work in all situations. One particular situation I would expect to fail is if the applet is dragged off the browser. I'm not sure if that is what you meant by 'standalone applet'.

    What would be a work around to make it work otherwise? Would you suggest there is not direct way to do so in the applet code itself?

    So far I have only been considering the typical, 'inbuilt' methods to communicate between applets. There are other ways to go about it.

    • Read/write cookies. One applet writes a cookie saying 'disable now', the other applet 'polls' for a cookie (tries to fetch it regularly, say every second or so) and if present, disables its own button. Note that AFAIR, the browser will prompt the user as to whether they want to allow the site to store cookies, so it would be best to explain to the user in advance that the applet is going to (or might) establish a cookie, and what that functionality supports. If a random site tries to make a cookie, I would generally refuse to allow it. Some users have cookies disabled by default. A sand-boxed applet can read/write cookies so long as both applets are deployed on the same domain.
    • Similar to the applet cookie mechanism (but in ways more reliable) is to use the JNLP API PersistenceService, it will prompt the user the first time it goes to save any information. The user can tick an 'always allow' box that persists for that session of the JVM. Either applet can read/write cookies from the same code base.
    • Sockets. I am pretty sure that an applet can establish a Socket, but it would take a trusted applet to establish a ServerSocket. Having said that, many machines have a firewall that the sockets must first contend with.