Results 1 to 6 of 6
  1. #1
    javaExprt is offline Member
    Join Date
    Dec 2012
    Posts
    3
    Rep Power
    0

    Exclamation Strange JFrame behaviour

    Hello
    I am not new to Java but I got a strange problem concerning JFrame AND java RMI
    would appriciate any help because it seems I miss something

    I created a small code with only the necesery code to show the problem

    Client is-a JFrame, implements IClient which is in turn an extension of the Remote interface (RMI) with only one method - to close the client, and in our case it will only unexport the object from RMI communication, and close the JFrame.

    Server is-a UnicastRemoteObject, implements IServer which is again extension of Remote, with remote method to connect a client (originaly it was couple of clients) to the server not by sending the client to the RMI registry.

    Idea is that when client creates itself it will invoke the remote method of the server, and send itself to the server.

    Second thing is that I create a button in the server JFrame so when I click on it, everything closes (in our case its only one server and one client).

    PROBLEM is that clicking on the button freeze the whole application ...

    I added in the code marks with (1) (2) and (3) in order to refer to the line after them:

    - remove comment after (1) and add comment after (2) , and it will work as intended (everything closes when clicking on the button)
    OR
    - un-comment (3) which will bypass the button-press, and will close everything, as intended

    This gives me the only idea that the problem is because of the dispose() plus the RMI communication, that somehow overlapping each other threads causing deadlock .. not sure how but I hope anyone knows why ?


    Java Code:
    import java.awt.event.ActionEvent;
    import java.rmi.Remote;
    import java.rmi.RemoteException;
    import java.rmi.registry.LocateRegistry;
    import java.rmi.server.UnicastRemoteObject;
    import java.util.Random;
    import javax.swing.AbstractAction;
    import javax.swing.JButton;
    import javax.swing.JFrame;
    
    public class ZNotWorking {
        static Random rnd = new Random();   
        
        public interface IClient extends Remote { public void CloseClient() throws RemoteException;  } 
        public interface IServer extends Remote { public void AddClient(IClient client) throws RemoteException;  } 
        
        public static class MyClient extends JFrame implements IClient{ 
            IServer server;
            public MyClient(IServer server){ // parameter only for testing that with line "this.server = server" it works
                super("client");
                setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
                try {
                    setVisible(true);
                    setLocation(rnd.nextInt(100)+100, rnd.nextInt(100)+100);
                    UnicastRemoteObject.exportObject(this, 0);
                    // (1)
                    //this.server = server;
                    // (2)
                    server = (IServer)LocateRegistry.getRegistry().lookup("server");
                    server.AddClient(this);
                } catch (Exception ex) {
                }
            }
            
            @Override
            public void CloseClient() throws RemoteException {   
                UnicastRemoteObject.unexportObject(this, true);    
                dispose(); // @note: problem centered here
            }
    
        };
        public static class MyServer extends UnicastRemoteObject implements IServer { 
            public JFrame form; 
            public IClient one_client;
            public MyServer() throws RemoteException { 
                super();
                form = new JFrame();
                form.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
                JButton button = new JButton("clickme");
                button.addActionListener(new AbstractAction() {
                    @Override
                    public void actionPerformed(ActionEvent e) {
                        CloseAll();
                    }
                });
                form.add(button);
                form.pack();
                form.setVisible(true); 
            }
    
            public void CloseAll() {
                try {
                    one_client.CloseClient();
                    form.dispose();
                } catch (Exception ex) {
                }
            }
            
            @Override
            public void AddClient(IClient client) throws RemoteException {
                one_client=client;
            }
    
        };
        
        public static void main(String[] args) {
            try {            
                LocateRegistry.createRegistry(1099);
                MyServer server = new MyServer();
                LocateRegistry.getRegistry().bind("server", server);
                //Server server = new Server();
                MyClient c = new MyClient(server);
                UnicastRemoteObject.unexportObject(server, true);  
                // (3)
                //server.CloseAll(); 
            
            } catch (Exception ex) {
            }
        }
    }


    Thanks if advance ! :)
    Last edited by javaExprt; 12-23-2012 at 11:16 PM. Reason: edit for making the problem more clear and concrete

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default Re: Strange JFrame behaviour

    I suspect that you are dealing with a Swing threading issue, and perhaps you're not using background threads from background processes, or are using background threads but are not updating GUI elements on the Swing thread, but this is only a guess. I'm not sure if your post contains enough information to allow us to be able to be able to do more than just guess, and you may need to show more code and post more information. The best code you could show us is an SSCCE, a small complete program that we can compile and run, that contains no code unrelated to your problem (the most challenging aspect of creating these) and that demonstrates your problem.

  3. #3
    javaExprt is offline Member
    Join Date
    Dec 2012
    Posts
    3
    Rep Power
    0

    Default Re: Strange JFrame behaviour

    Thanks for the answer. I made an entire edit of the post which the SSCCE notion, also making it more clear to myself what the problem is about, yet was not able to resolve it :(

  4. #4
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default Re: Strange JFrame behaviour

    You're not doing background processes on background threads but instead are running them in the Swing thread. For instance:

    Java Code:
          public void CloseAll() {
             new Thread(new Runnable() {
                public void run() {
                   try {
                      one_client.CloseClient();
                   } catch (RemoteException e) {
                      e.printStackTrace();
                   }
                }
             }).start();
    
             form.dispose();
    
          }

  5. #5
    javaExprt is offline Member
    Join Date
    Dec 2012
    Posts
    3
    Rep Power
    0

    Default Re: Strange JFrame behaviour

    Quote Originally Posted by Fubarable View Post
    You're not doing background processes on background threads but instead are running them in the Swing thread. For instance:

    Java Code:
          public void CloseAll() {
             new Thread(new Runnable() {
                public void run() {
                   try {
                      one_client.CloseClient();
                   } catch (RemoteException e) {
                      e.printStackTrace();
                   }
                }
             }).start();
    
             form.dispose();
    
          }
    Wow, a big THANK YOU for that
    I never heard of the term 'Swing Thread' so I googled and read it has something about taking all the swing components to run under one big thread ? Why did it has something to do with my problem ? Can you enlighten me ? :) Why I need a NEW thread just to close the client ?
    Thanks again

  6. #6
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default Re: Strange JFrame behaviour

    The Swing event thread, known as the Event Dispatch Thread or EDT is responsible for handling all of the GUI's graphics and user interactions, and so if it is encumbered by a long-running task, the GUI will effectively freeze. For this reason all long-running tasks need to be performed in a background thread, often with the aid of a SwingWorker object. For more on this, please see Concurrency in Swing.

Similar Threads

  1. Strange behaviour
    By imadabh in forum Threads and Synchronization
    Replies: 1
    Last Post: 05-11-2011, 04:31 PM
  2. Strange JVM behaviour
    By pjpr in forum Advanced Java
    Replies: 13
    Last Post: 01-03-2011, 08:39 PM
  3. Strange behaviour in serialization
    By Wolverine in forum Networking
    Replies: 0
    Last Post: 05-23-2009, 01:03 PM
  4. AffinedTransform strange behaviour
    By Echilon in forum AWT / Swing
    Replies: 3
    Last Post: 12-11-2008, 10:58 AM
  5. Strange behaviour in swing
    By cbalu in forum AWT / Swing
    Replies: 1
    Last Post: 05-23-2008, 10:23 PM

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •