Passing JFrame via RMI: isShowing() returns false on client but true on server?

StackOverflow https://stackoverflow.com/questions/21782326

  •  11-10-2022
  •  | 
  •  

문제

Can anyone explain why the below code prints this?: Server: true Client: false

isShowing() is just a getter for a boolean defined in Component - shouldn't that serialize properly?

RMIInterface.java

public interface RMIInterface extends Remote {
    public JFrame getJFrame() throws RemoteException;
}

RMIClient.java

public class RMIClient {
    public static void main(String args[]) throws Exception {
        Registry registry = LocateRegistry.getRegistry(1099);
        RMIInterface server = (RMIInterface) registry.lookup("myService");
        JFrame frame = server.getJFrame();
        System.out.println("Client: " + frame.isShowing());
    }
}

RMIServer.java

public class RMIServer implements RMIInterface {
    private JFrame frame;

    protected RMIServer() {
        createAndShowGUI();
    }

    private void createAndShowGUI() {
        frame = new JFrame("FrameDemo");
        frame.pack();
        frame.setVisible(true);
    }

    @Override
    public JFrame getJFrame() {
        System.out.println("Server: " + frame.isShowing());
        return frame;
    }

    public static void main(String[] args) throws Exception {
        Registry registry = LocateRegistry.createRegistry(1099);
        RMIServer server = new RMIServer();
        RMIInterface stub = (RMIInterface)UnicastRemoteObject.exportObject(server, 0);
        registry.bind("myService", stub);
    }
}
도움이 되었습니까?

해결책

You are mixing up isShowing() and isVisible(). visible is just a boolean property but showing implies actually showing on the screen and AWT components are never showing when they are just de-serialized. You have to invoke setVisible(true) on the Window ancestor of the component hierarchy (i.e. the JFrame) after de-serialization to make it showing.


Just consider Component.isShowing():

Determines whether this component is showing on screen. This means that the component must be visible, and it must be in a container that is visible and showing.

and Window.isShowing():

Checks if this Window is showing on screen.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top