File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Swing / AWT / SWT and the fly likes JButton behaviour for JPopupMenu Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "JButton behaviour for JPopupMenu" Watch "JButton behaviour for JPopupMenu" New topic

JButton behaviour for JPopupMenu

Faisal Ul-Haque

Joined: Dec 02, 2011
Posts: 10
Hi All,
I would like to ask if anybody has an explanation and solution for the following problem.

I have a JPopupMenu activated by a JButton and it is exhibiting the following functionality:

1. When the JPopupMenu is not visible and the JButton is clicked --> shows JPopupMenu (AS EXPECTED)
2. When the JButton is clicked after the JPopupMenu is made visible and discarded by clicking anywhere else --> shows JPopupMenu (AS EXPECTED)
3. When the JButton is clicked after the JPopupMenu is made visible --> flickers (hides and shows) the JPopupMenu (NOT DESIRED FUNCTIONALY)

My frustration is scenario 3 and if anyone has an explanation and solution it would be appreciated.

My test class is:
Darryl Burke

Joined: May 03, 2008
Posts: 5115

In the button's actionPerformed, it's pointless to test whether the popup menu is visible, as the action of clicking the button hides the popup. This happens before the button is released and the actionListener code executes.

You might like to disable the button and re-enable it when the popup is hidden.

Edit: No, that's not a solution. The button becomes enabled because a moue down over the disabled button hides the popup.

Edit2: Queuing the enabling of the button solves the problem. Code updated.

A better approach might be to use a JToggleButton.

luck, db
There are no new questions, but there may be new answers.
I agree. Here's the link:
subject: JButton behaviour for JPopupMenu
It's not a secret anymore!