Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Modal window

Status
Not open for further replies.

scienzia

Programmer
Feb 21, 2002
160
IT
Hello,

I would like to show a popup window saying "WAIT" during an operation and I would like to close it when it is finished. The program window should be impossible to use during the operation. How can I do this????

PS: the window shouldn't have any close button
 
The following class does most of what you are looking for.

Code:
class WaitFrame extends JFrame
{

  JFrame mFrame = this;
  Thread mThread;

  public WaitFrame(Thread t)
  {
    mFrame = new JFrame();
    mThread = t;

    // setup frame
    JLabel lblMsg = new JLabel("Please Wait...");

    Container contentPane = mFrame.getContentPane();
    contentPane.setLayout(new GridBagLayout());
    contentPane.add(lblMsg);

    mFrame.setSize(new Dimension(200,100));
    mFrame.setVisible(false);
    mFrame.setDefaultCloseOperation(mFrame.DO_NOTHING_ON_CLOSE);
  }

  public void doWait()
  {
    mFrame.show();

    // start the thread
    mThread.start();

    // make sure the current frame is painted
    Graphics g = mFrame.getGraphics();
    mFrame.paint(g);

    // join the thread (until it finishes or is interrupted)
    try
    {
      mThread.join();
    }
    catch (InterruptedException ie)
    {}

    // close the frame
    mFrame.dispose();
  }
}

then to use it...

Code:
// thread to do whatever
Thread t = new Thread()
{
  public void run()
  {
    try
    {
      sleep(3000);
    }
    catch (Exception e)
    {}
  }
};

WaitFrame wf = new WaitFrame(t);
wf.doWait();

Problems I can think of:
1. Clicking on a button in another frame during the wait will not immediately register, but will be executed right after the wait screen closes.
2. The frames are not repainted during the wait.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top