1

NOTE: I work a lot of hours and research google and stackoverflow but I cannot find answer.

I use Thread.sleep() in a JDialog and it freezes all other JDialog, JFrame and threads.

My example code:

public Guitest()
{
    setSize(300,300);


    // create a JDialog that make guitest wait
    MyDialog dlg = new MyDialog();
    dlg.setSize(100,100);
    dlg.setVisible(true);

    while(dlg.isWait())
    {
        try
        {
            Thread.sleep(1000);
        } catch (InterruptedException ex)
        {
            Logger.getLogger(Guitest.class.getName()).log(Level.SEVERE, null, ex);
        }
        System.out.println("waiting mydialog");
    }


}


class MyDialog extends JDialog
{
    boolean wait = true;
    JButton btn = new JButton("OK");

    public MyDialog()
    {
        setSize(50,50);

        btn.addActionListener(new ActionListener()
        {

            @Override
            public void actionPerformed(ActionEvent e)
            {
                wait=false;
            }
        });
        add(btn);
    }

    public boolean isWait()
    {
        return wait;
    }
}

In this situation JDialog does not appear correctly:

inccorect appear jdialog

But it must be apper this:

true appear jdialog

How can I solve this problem. I want to make main thread wait another thread.And someone can correct my sample code or share a sample code with this situation.

Zava
  • 400
  • 3
  • 16

2 Answers2

1

IMHO, it appears like you have just one running thread. At first, we draw our JDialog, after that, you sleep your main thread because of the wait flag. ie. you can't execute your button action listener, thus you can't awake your thread.

Hope it helps understanding.

Zava
  • 400
  • 3
  • 16
  • I don't think so, you have to implement it yourself. I found this thread : http://stackoverflow.com/questions/20269083/make-a-swing-thread-that-show-a-please-wait-jdialog – Zava Jan 12 '16 at 11:00
  • Thank you for asnwer, I understand. I should learn a lot of thing in Java. :) – Halil İbrahim Oymacı Jan 12 '16 at 11:05
1

Thread.Sleep() just sleeps the current thread (i.e. stops it from doing anything, such as redrawing, processing clicks etc), which in your case is the UI thread.

You need to use a worker thread. Any main work that needs to be done that may take a larger amount of time needs to be done in its own thread, and this is the thread that you will want to sleep. It is currently ran alongside the UI components and so this is why you're seeing them freezing.

A good reference is the documentation for concurrency for swing http://docs.oracle.com/javase/tutorial/uiswing/concurrency/

The following may be useful too:

http://java.sun.com/developer/technicalArticles/Threads/swing/ http://java.sun.com/products/jfc/tsc/articles/threads/threads1.html

Smittey
  • 2,475
  • 10
  • 28
  • 35
  • They both run on the `UI thread`, which is getting locked up by `Thread.sleep()`. When executed within on a `worker thread`, it will have a different impact, but it's currently ran alongside UI components, which is why you're seeing them locked up! – Smittey Jan 12 '16 at 11:08