Search code examples
javamodel-view-controllerswinguser-interfacermi

Passing Swing classes (references) on network?


I want to know that is it possible to send major Swing classes event/actionlisteners, Events, components via RMI.

Possible scenario: If one client press the button or move the slider every client's button or slider move etc same for other swing elements.

I am expecting the answer in the context of RMI and swing MVC architecture, i want to call the swing component's models e.g ButtonModel and want to send swing ActionEvent on wire and register PropertyChangeListener/PropertyChangeSupport as remote objects for getting updates at client site.

typical examples : the server should call this method for each client, when ever some change occur in model

 public void propertyChange(PropertyChangeEvent evt) {
        for (AbstractViewPanel view: registeredViews) {
            view.modelPropertyChange(evt);
        }
    }

in case of an event on one client, each client actionPerformed should be called from server:

@Override
public void actionPerformed(ActionEvent e)  {
}

is it feasible? if not then why? where i could face the problems, i mean which classes are transferable (serialized) and which are not...

EDIT: here you see i m invoking Java Swing defaultbuttonmodel remotely, the only thing left when some of it's property or method change the other client's get updates, best would be following swing propertychangelistener if someone can just help me, realizing this, it would be great:

public class RemoteButtonModel extends UnicastRemoteObject implements Model {

    private ButtonModel model = new DefaultButtonModel() ;

    protected myModel() throws RemoteException {
        super();
    }


    @Override
    public void setEnabled(boolean b) throws RemoteException {
        if (isEnabled())
            model.setEnabled(false);
        else{
            model.setEnabled(true);   
           }
    }

    @Override
    public boolean isEnabled() throws RemoteException {
        return model.isEnabled();

    }
}

Solution

  • The Javadoc for every Swing class says that it should not be serialized.

    More probably you should be transmitting the associated Model classes.

    And event listening via RMI is an anti-pattern. Too much traffic, too many points of failure.