title: Java swing GUI components not showing up after Observer update() notified from a separate thread
body: <p>My application basically uses apache's httpclient to connect to a server and do something for the user.</p>
  
  <p>I am a java beginner (though not a development beginner), so many java specific approaches may have been calmly ignored.</p>
  
  <p><strong>DESCRIPTION</strong></p>
  
  <ul>
  <li>the app uses a TrayIcon with a popup menu as the only user interface and reacts with either tray messages or popup [confirm] dialogs.</li>
  <li>it uses threads because the httpclient was effectively blocking the main thread when waiting for a response</li>
  <li>employs a client-server to assure a single instance only and reactions to second instance attempts</li>
  <li>employs observer/observable pattern to react to the httpclient's and the single instance watchdog server's messages</li>
  <li>the main behavior is controlled by a <strong>Controller</strong> class, which is the only Observer; everything related to the httpclient is in a separater <strong>WebClient</strong> class, which implements Runnable and extends Observable; then there's the <strong>AttServer</strong> which is again Runnable and Observable and controls everything around the server listener (there's not much actually).</li>
  </ul>
  
  <p>This is my first application in Java, so many obscurities are possible..:(</p>
  
  <p><strong>PROBLEM</strong></p>
  
  <p>The desired behavior is that whenever a user attempts to run a second instance of this app, then it tries to bind a socket listener on port X and, failing, sends a string message to that port. The 'main' app accepts the message and reacts with a confirm dialog being shown asking the user for some input.</p>
  
  <p>Everything was working just fine up to the point when I've added the client-server; the second instance successfully triggers the update() method of the observer, the observer correctly distinguishes what's going on and fires the corresponding method; and then at the point of the first new GUI component (a confirm dialog) should show up, NOTHING happens:</p>
  
  <ul>
  <li>the app doesn't do anything</li>
  <li>no errors or exceptions thrown in System.out</li>
  <li>the tray icon CONTINUES to work just fine (right click shows a menu etc)</li>
  </ul>
  
  <p>..it's just that all the instructions coming after the dialog doesn't get executed and the dialog doesn't show up.</p>
  
  <p><strong>WEIRDNESS #1</strong></p>
  
  <p>When the user uses the tray icon menu to fire some other command that shows a dialog, the GUI somehow coughs up and shows BOTH (or more when appropriate) dialogs in the correct order (first the stuck hidden dialog shows up and then the second fresh one) and everything just seems that this is the correct state and all commands get executed correctly (albeit some of them 'a bit' later).</p>
  
  <p><strong>WEIRDNESS #2</strong></p>
  
  <p>I guess this is caused by the swing being not really compatible with threads; however, when the update() method gets fired up from the httpclient (which is a separate thread as well), the GUI works just fine. The only difference in the way the Observer gets notified is that the <strong>WebClient</strong> calls the notifyObservers() after some user action (albeit in a separate thread and after (at worst) 30s timeout and the <strong>AttServer</strong> calls notifyObservers() in a catch() block, because IOException is thrown when the specified port has been already bound.</p>
  
  <p><strong>WEIRDNESS #3</strong></p>
  
  <p>This problem is irregular to say the least. It USUALLY occurs in the development environment (netbeans), although sometimes it doesn't --- at first I've got the impression it was solved by calling another (dummy) JDialog, which remained hidden intentionally, before the actual problematic dialog --- it seemed that swing needed a little 'nudge outta the door'; though after building the project into a jar or an exe (via launch4j), the problem was back. Then I've tried to use a single JFrame component for ALL the swing dialogs and it helped somehow --- sometimes when the output jar is run it works as it should and the next day it hides dialogs again.</p>
  
  <p>I am totally lost, scoured the Earth for a solution, found nothing really. I hope that somebody else might bump into some similar problem and, being a more experienced Javaman, found a solution. I've also tried some doLayout(), validate(), revalidate(), repaint(), update() calls on the main JFrame instance, but no go.</p>
  
  <p>There's hope :) Thanks for reading and thanks even more for any ideas.</p>
  
  <p><strong>CODE EXAMPLES</strong></p>
  
  <p>This is the run() function from <strong>AttServer</strong> -- the source of the non-working update()s.. (it's a bit hard coded as the main functionality is there and I got stuck elsewhere..)</p>
  
  <pre><code>public void run() {
  
      this.addObserver( Controller.getInstance() );
  
      setChanged();
  
      // this.command == INIT set and new thread with AttServer called in the beginning of the main()
      if( null != this.command ) {
  
          switch( this.command ) {
              case INIT:
                  try {
                      init();
                  } catch( IOException ex ) {
                      notifyObservers( new ErrorEvent( 100 , "Could not bind the specified port." ) );
                  }
              break;
              default:
          } //switch
  
      // no command, looks like an incoming connection
      } else {
  
          try {
              DataInputStream in = new DataInputStream( socket.getInputStream() );
  
              String inputLine;
  
              while( ( inputLine = in.readLine() ) != null ) {
  
                  if( inputLine.trim().equals( EventType.CLOCKIN.toString() ) ) {
  
                      notifyObservers( EventType.CLOCKIN );
  
                      return;
                  }
              }
          } catch( IOException ex ) {
              //todo
          }
      }
  }
  </code></pre>
  
  <p>..this is the opposite side of the fence, the update() method:</p>
  
  <pre><code>public void update( Observable obj, Object arg) {
      // received a remote request *******************************************
      if( obj instanceof AttServer
          &amp;&amp; arg instanceof EventType ) {
  
          EventType command = (EventType) arg;
  
          Attendance.debugMsg( "Received " + command.toString() + " request from AttServer" );
  
          if( false == isOnClock ) {
              doClock( true , true );
          }
      } // ..........
  </code></pre>
  
  <p>.. and this is the essence of the doClock() method called from update() -- there's nothing really, and even when the dialog is shown right after the call, or event when it's shown right in the update() method, it doesn't work.</p>
  
  <pre><code>(overloaded with default data)
  public static void doClock( Boolean clockIn , Boolean confirm , String message , String title ) {
  
      Event lastEvent = Log.getLastClock();
  
      if( confirm ) {
          if( JOptionPane.NO_OPTION == JOptionPane.showConfirmDialog( mainFrame , message , title , JOptionPane.YES_NO_OPTION ) ) {
              return;
          }
      }
  </code></pre>