jQuery in Action, 2nd edition*
The moose likes Swing / AWT / SWT and the fly likes Menu not closing directly Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "Menu not closing directly" Watch "Menu not closing directly" New topic
Author

Menu not closing directly

colin shuker
Ranch Hand

Joined: Apr 11, 2005
Posts: 744
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

Joined: Jan 14, 2004
Posts: 1535
colin shuker
Ranch Hand

Joined: Apr 11, 2005
Posts: 744
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

Joined: Apr 11, 2005
Posts: 744
Ok, I understand it, except for:

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

Thanks
Craig Wood
Ranch Hand

Joined: Jan 14, 2004
Posts: 1535
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.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Menu not closing directly