Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Cloud/Virtualization forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Menu not closing directly

 
colin shuker
Ranch Hand
Posts: 750
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi, I have a GUI for a chess program, the GUI class extends JFrame and implements ActionListener

The data members are:
JMenu fileMenu;
JMenuItem NEW=new JMenuItem("New");
JMenuItem EXIT=new JMenuItem("Exit");

and the JMenu is added to a JMenuBar object.

Now the actionPerformed(..) method shown below:

calls the doSomeMethod() when the NEW option is selected.
However the Menu does not close till doSomeMethod() completes.

This is slightly annoying in my situation sine doSomeMethod() will take
several seconds or more to complete(program is thinking bestmove), and
the GUI looks awquard with the menu just hanging there while program
thinks of move to play.

Is there anyway to close this menu first, before calling the doSomeMethod() method.

Thanks for any help.
 
Craig Wood
Ranch Hand
Posts: 1535
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
 
colin shuker
Ranch Hand
Posts: 750
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks so much, you are amazing at this GUI stuff.
I am allergic to threads, so this would have taken me months/years to figure out.
I just ran the program, and it works great.
I'm just reading through it all step by step, so I understand it properly

I'll let you know if I theres something I don't understand.

Thanks again for your time and effort
 
colin shuker
Ranch Hand
Posts: 750
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ok, I understand it, except for:

Is the synchronized necessary, can you explain briefly its purpose?

Thanks
 
Craig Wood
Ranch Hand
Posts: 1535
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The "synchronized" key word and its use are discussed in Threads: Doing Two or More Tasks at Once which is linked from
How to Use Threads in the swing tutorial. This second page contains a brief discussion of "The Event-Dispatching Thread".
In the test app the calculateNextMove method can be called multiple times by clicking on one or both of the menu items while the method thread is running. To observe this try running the app with and without the "synchronized" modifier. Without the modifier things get more chaotic. Using the "synchronized" modifier makes it where the method can be used by only one caller at a time. Others must wait in line until the current caller releases its lock on the method; then the next in line can lock the method and use it. It's a way to schedule calls to the method.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic