title: JavaFx and socket listener
body: <p>we are developing a client-server application which has to work both with GUI and CLI.
  We have no problem with the CLI but we are struggling to implment it with JavaFX:</p>
  
  <p>our server send some objects to the client (via socket) and these have to be processed.</p>
  
  <p>This is our SocketServerListener (and writer):</p>
  
  <pre><code>public class SockeServerListener extends Thread implements ServerListener{
      private CliController controller;
      private ObjectInputStream in;
      private ObjectOutputStream out;
  
      public SocketServerListener(Socket server, CliController controller) throws UnknownHostException, IOException {
          this.controller = controller;
          this.out = new ObjectOutputStream(server.getOutputStream());
          this.in = new ObjectInputStream(server.getInputStream());
      }
  
      public void publishMessage(String message) throws IOException, RemoteException {
          out.writeObject(message);
          out.flush();
      }
  
      public void run() {
          try {
              while (true) {
                  Dialogue dialogue = (Dialogue) in.readObject();
                  controller.parseDialogue(dialogue);
              }
          } catch (IOException e) {
              // TODO Auto-generated catch block
              e.printStackTrace();
          } catch (ClassNotFoundException e) {
              // TODO Auto-generated catch block
              e.printStackTrace();
          }
      }
  
  
  }
  </code></pre>
  
  <p>The SocketServerListener is instantiated by the controller which is the one who executes the who "executes" the received object, updating the interface (Cli/GUI)</p>
  
  <pre><code>public void parseDialogue(Dialogue dialog) {
          dialog.execute(view); //view is an interface extended by both the Cli and GUI
          this.canWrite = true;
      }
  </code></pre>
  
  <p>As I said, this works very well with the CLI, but it throws an exception with JavaFX</p>
  
  <pre><code>Exception in thread "Thread-7" java.lang.IllegalStateException: Not on FX application thread; currentThread = Thread-7
  </code></pre>
  
  <p>We tried to use the Class used to generate all the GUI as controller, but without any (positive) result.
  How can we instantiate a thread so that it can work with JavaFX and call the methods that show the screens we need?</p>
  
  <p>Thank you</p>