title: Java invokeAndWait of C# Action Delegate
body: <p>the issue i mentioned in <a href="https://stackoverflow.com/questions/2849762/java-runtime-exception">this post</a> is actually happening because of cross threading GUI issues (i hope).</p>
  
  <p>could you help me with Java version of action delegate please?</p>
  
  <p>in C# it is done as this inline:</p>
  
  <pre><code>        this.Invoke(new Action(delegate()
          {...}));
  </code></pre>
  
  <p>how is this achived in Java?</p>
  
  <p>thank you.</p>
  
  <pre><code>public class processChatMessage implements Observer {
  
      public void update(Observable o, Object obj) {
  
  
          System.out.println("class class class" + obj.getClass());
  
          if (obj instanceof String){
  
              String msg = (String)obj;
  
              formatChatHeader(chatHeader.Away, msg);
  
              jlStatusBar.setText("Message Received");
  
              // Show chat form
              setVisibility();
  
          }
      }
  }
  </code></pre>
  
  <p>processChatMessage is invoked by a separate thread triggered by receiving new data from a remote node.</p>
  
  <p>and i think the error is being produced as it trying to update GUI controls.</p>
  
  <p>do you think this is the reason? i ask because im new to Java and C#, but this is what is going on i think.</p>
  
  <p><strong>SOLUTION:</strong></p>
  
  <pre><code>public class processChatMessage implements Observer {
  
      public void update(Observable o, Object obj) {
  
          if (obj instanceof String){
  
              final String msg = (String)obj;
  
              try {
  
                  SwingUtilities.invokeAndWait(new Runnable( ) {
  
                      public void run( ) {
  
                          formatChatHeader(chatHeader.Away, msg);
                          jlStatusBar.setText("Message Received");
                          setVisibility();
                      }
                  });
              } catch (InterruptedException e){
  
              } catch (InvocationTargetException e){
  
              }
          }
      }
  }
  </code></pre>