This week's giveaway is in the Spring forum.
We're giving away four copies of REST with Spring (video course) and have Eugen Paraschiv on-line!
See this thread for details.
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

Win a copy of REST with Spring (video course) this week in the Spring forum!
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: 5092

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.
Don't get me started about those stupid light bulbs.
subject: JButton behaviour for JPopupMenu
jQuery in Action, 3rd edition