how to avoid GUI blocking on main thread

[nobr]hi,

I use this code in my main()

publicstaticvoid main(String[] args)

{

CylonBar bar =new CylonBar();

bar.start();

SwingUtilities.invokeLater(new Runnable(){

publicvoid run(){

Options op =new Options();

OdessaClient oc =new OdessaClient(op);

oc.setVisible(true);

}

});

bar.setStopFlag(true);

}

CylonBar is a thread which loads a Jframe with a progress bar in it.

The problem is that although it is loading, the progress bar is blocked.

It gets unblocked only after the main Jframe stops loading...

this is the code for my CylonBar thread

publicclass CylonBarextends Thread{

JProgressBar bar =new JProgressBar();

JFrame frame;

volatile Boolean stopflag =false;

public CylonBar()

{

frame =new JFrame();

frame.getContentPane().setLayout(new BorderLayout());

frame.getContentPane().add(bar, BorderLayout.CENTER);

frame.setLocation(Toolkit.getDefaultToolkit().getScreenSize().width/4, Toolkit.getDefaultToolkit().getScreenSize().height/4);

frame.add(bar, BorderLayout.NORTH);

frame.add(new JLabel("<html><b>Application is loading<br>Please wait...</b></html>"), BorderLayout.CENTER);

bar.setIndeterminate(true);

frame.pack();

frame.setVisible(true);

}

publicvoid run()

{

while (true)

{

try{

sleep(1000);

if(stopflag){

frame.setVisible(false);

}

}catch (InterruptedException e){}

}

}

publicvoid setStopFlag(boolean t)

{

this.stopflag = t;

}

}

Why is this happening and how can I avoid it?[/nobr]

[3481 byte] By [xpantaa] at [2007-11-27 3:19:25]
# 1

The creation of your OdessaClient will run in the AWTEvent-Queue thread. SwingUtilites.invokeLater adds the creation Runnable to the working queue for the AWTEvent-Queue thread. So the CeylonBar GUI will not be "served" before the invoked Runnable is done.

Is it possible for you to put the non GUI related loading of OdessaClient to another working thread ?

KlausJJa at 2007-7-12 8:22:05 > top of Java-index,Core,Core APIs...
# 2
See the SwingWorker class.
Niceguy1a at 2007-7-12 8:22:05 > top of Java-index,Core,Core APIs...
# 3
> Is it possible for you to put the non GUI related> loading of OdessaClient to another working thread ?I could if I know how to. Any suggestions?Before going to SwingWorker (I don't know if it is compatible with 1.5 VM version) I need to try the "old way"...
xpantaa at 2007-7-12 8:22:05 > top of Java-index,Core,Core APIs...
# 4

Have you tried to create the OdessaClient in the main instead of the AWTEvent-Queue thread?

CylonBar bar = new CylonBar();

bar.start();

Options op = new Options();

OdessaClient oc = new OdessaClient(op);

SwingUtilities.invokeLater(new Runnable(){

public void run(){

oc.setVisible(true);

}

});

bar.setStopFlag(true);

KlausJJa at 2007-7-12 8:22:05 > top of Java-index,Core,Core APIs...
# 5

> > Is it possible for you to put the non GUI related

> > loading of OdessaClient to another working thread

> ?

>

>

> I could if I know how to. Any suggestions?

>

> Before going to SwingWorker (I don't know if it is

> compatible with 1.5 VM version) I need to try the

> "old way"...

SwingWorker was available as a standalone class before it was added into the JDK.

http://java.sun.com/products/jfc/tsc/articles/threads/threads1.html

Niceguy1a at 2007-7-12 8:22:05 > top of Java-index,Core,Core APIs...
# 6
... which was about nine years ago ...
ejpa at 2007-7-12 8:22:05 > top of Java-index,Core,Core APIs...
# 7

> Have you tried to create the OdessaClient in the main

> instead of the AWTEvent-Queue thread?

>

> > CylonBar bar = new CylonBar();

> bar.start();

> Options op = new Options();

> OdessaClient oc = new OdessaClient(op);

> SwingUtilities.invokeLater(new Runnable(){

> public void run(){

> oc.setVisible(true);

> }

> });

> bar.setStopFlag(true);

>

Hi, I tried this solution and seems to work. The only addition I had to make was to instantiate OdessaClient instance as 'final'. Why I had to do this?

xpantaa at 2007-7-12 8:22:05 > top of Java-index,Core,Core APIs...
# 8

Sorry, I think this idea was not so good, because creation of the GUI should also run in the AWTEventThread. With my suggested change the application may be unstable. Swing examples in tutorial always suggest to put GUI creation and showing code in the run() methodto avoid thread conflicts.

Why do you need a progress bar ? Does the creation of OdessaClient take so much time ?

KlausJJa at 2007-7-12 8:22:05 > top of Java-index,Core,Core APIs...
# 9

No I have never been asked to use a progress bar. I just thought it might be a good idea and a chance for me to look into threads (which I 've never done in the past).

However I have learned more things about SwingWorker (mentioned above) and seems to be very neat. I will try that too.

Thank you very much for your input!

xpantaa at 2007-7-12 8:22:05 > top of Java-index,Core,Core APIs...