//  conceptGO Open Source - www.conceptGO.com
//  copyright (c)  2003, conceptGO
//
//  This software is licensed under the 
//  conceptGO Open Source License, available 
//  at http://www.conceptgo.com/cgooslicense.html
//

// UseMainThread - Continue startup with main thread

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;

public class UseMainThread extends    JFrame
                           implements ActionListener,
                                      WindowListener
{
  volatile boolean bLoaded1 = false; // load1 done?

  JButton jbPressMe = new JButton("Press Me");
  JComboBox jcb = new JComboBox();
  JLabel jlName = new JLabel("Name:");
  JTextField jtfName = new JTextField(15);
  JTextArea jta = new JTextArea( 5, 30 );

  public UseMainThread()
  {
    super( "Main Thread Processing" );
    setResizable(false);
    addWindowListener( this );

    jta.setEditable( false );

    JPanel jpNorth = new JPanel();
    jpNorth.add( new JScrollPane( jta ) );

    jbPressMe.addActionListener( this );
    jcb.setEnabled( false );

    JPanel jpCenter = new JPanel();
    jpCenter.add( jlName );
    jpCenter.add( jtfName );
    jpCenter.add( jbPressMe );
    jpCenter.add( jcb );

    Container myCP = getContentPane();
    myCP.add( jpNorth, BorderLayout.NORTH );
    myCP.add( jpCenter, BorderLayout.SOUTH );

    setLocation( 100, 100 );
    pack();
    show();

    // continue to other tasks after AWT thread start
    Thread.currentThread().setPriority( Thread.MIN_PRIORITY );
    sleeper(1);
    load1();
    load2();
  }  // end constructor

  void load1()
  {
    // simulate a time taking task
    sleeper(5000);  // 5 seconds

    jta.append("load1 complete." + "\n" );
    bLoaded1 = true;
  }  // end load1

  void load2()
  {
    // simulate a time taking task
    sleeper(2000);

    jta.append("load2 complete." + "\n" );
    jcb.setEnabled( true );

    Thread.currentThread().setPriority( Thread.NORM_PRIORITY );
  } // end load2

  void sleeper( int iValue )
  {
    try { Thread.sleep( iValue ); }
    catch( InterruptedException ie ) {}
  } // end sleeper

  public void actionPerformed(ActionEvent e)
  {
    Object oSource = e.getSource();

    if( oSource == jbPressMe )
    {
      if( !bLoaded1 )
      {
        setCursor( Cursor.getPredefinedCursor( 
                             Cursor.WAIT_CURSOR ));
        // if not complete, wait
        while( !bLoaded1 ) { sleeper(50); }
        setCursor( Cursor.getDefaultCursor() );
      } // end if !bLoaded1

      // now do work associated with button
      jta.append("From Press Me: I was pressed." + "\n" );
      return;
    } // end if jbPressMe
  } // end actionPerformed

// Window Listener Implementation
  public void windowOpened(WindowEvent e)
  {
    jta.append( "Window Opened." + "\n" );
  }
  public void windowClosing(WindowEvent e)
  {
    dispose();
    System.exit(0);
  }
  public void windowClosed(WindowEvent e) {}
  public void windowIconified(WindowEvent e) {}
  public void windowDeiconified(WindowEvent e) {}
  public void windowActivated(WindowEvent e) {}
  public void windowDeactivated(WindowEvent e) {}
// End Window Listener Implementation

  public static void main(String[] args)
  {
    new UseMainThread();
  }

}  // end class UseMainThread

Copyright © 2000-2003, conceptGO