Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / General / December 2005

Tip: Looking for answers? Try searching our database.

GUI locking up, but code running fine.

Thread view: 
prolifekilledmywife@yahoo.com - 28 Dec 2005 21:18 GMT
I'm using Netbeans 5.0 beta for an IDE, with swing components. The
situation is something like this:

The program is a board game - the user chooses a building from a panel,
and then is supposed to get a message to choose what resource to pay
for it with.  Here's a trace of the code:

Game (main object):
      ....
      board[i].activate():
                     Game.game.setPlayer(worker);
                     Game.game.playerMessage("Choose a wooden building
from the building panel.");
                     Game.game.waitForBuild(type) :
                                      built = false;
                                      state = type + Game.BuildWood -
1;

mainPanel.buildings.getBP().setSelectedIndex(type-1);
                                      while(!built)
                                      { Thread.yield(); }
No problems yet.  This works fine, unless a building that needs to call
chooseResource is selected.
So, the user clicks one of these buildings, which activates the
following code, which is where the freeze occurs:

                    String r = Game.game.chooseResource():
                                   state = Game.chooseResource;
                                   resource = "";
                                   while(resource.length()==0)
Thread.yield();
                                   return resource;

Now chooseResource does work in any other context - resource gets set
by a mouseClicked event handler in a panel out there.  But at this
point, the GUI stops responding.  The playerMessage never gets printed,
and the click event never gets triggered.  I did some investigating,
printing out numbers inside the two inmost loops, and the
chooseResource loop is continually running.  I know the code's ugly,
but even so, the cause of this behavior is beyond me.  Any suggestions
would be greatly appreciated!
Chris Smith - 29 Dec 2005 00:15 GMT
>                      String r = Game.game.chooseResource():
>                                     state = Game.chooseResource;
[quoted text clipped - 10 lines]
> chooseResource loop is continually running.  I know the code's ugly,
> but even so, the cause of this behavior is beyond me.

The loop runs forever because nothing is causing resource.length() to
return anything other than 0.  It blocks the GUI because this code is
running in the thread that does all the GUI stuff.  Remaining GUI work
is all queued up waiting for this code to finish so that it has a chance
to run.  You should move the long-standing work into a separate thread
(although there really isn't long-standing work, just a busy-wait loop.)

This is a case where ugly code isn't something you should try to
convince us to ignore so we can get to the real problem.  Ugly code is
the cause of the problem.  Take some pride in your work, and do things
right.  If you know this is bad code, don't write it.

Signature

www.designacourse.com
The Easiest Way To Train Anyone... Anywhere.

Chris Smith - Lead Software Developer/Technical Trainer
MindIQ Corporation

prolifekilledmywife@yahoo.com - 29 Dec 2005 00:34 GMT
Thanks for the response, Chris.  I sorted it out though - the problem
was actually that the GUI wouldn't do anything until the mouseClicked
event returned.  Make sense, I guess. Resource does get set by the
mouseClicked method of the player board - I guess I probably should
have mentioned that as well.  Anyway, thanks again.


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.