views:

63

answers:

2

Hi,

In my application I have a frame, with toolbar (the toolbar contains some actions). I want the toolbar to be visible only when the window is focused. So, I registered a windowFocusListener on the window. The problem is- when the window is not focused and I click on the place where a tool bar action should be- the action is performed. This happens because the WindowFocusGained is called before the mouse button is released and when the mouse button released it calls the actionPerformed.

Does anybody has any idea for a work around for this problem? Does anybody know how to determine wether the mouse button is clicked now?

A: 

You could add a MouseListener to the window, and check if the toolbar is visible in the mousePressed event. If the toolbar is not visible at the time the mouse is pressed, set a flag on the toolbar (something like "ignoreNextAction").

In the toolbar, check that flag in your actionPerformed event handler.

Reset the toolbar flag in the mouseReleased event on the window, so that the next click will work correctly.

This is assuming the mouseReleased event on the window happens after the actionPerformed on the toolbar (not sure about this). Worst case, you can work with a timer which resets the flag 50ms after the mouse event.

Alexander Malfait
A: 

You could now only hide the toolbar when the window loses focus, but also disable all buttons with setEnabled(false), then re-enable on focus gain. Alternatively, synchronize setEnabled(..) with the visibility of the buttons (instead of the window focus).

Thomas