Win a copy of Mesos in Action this week in the Cloud/Virtualizaton forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

JButton behaviour for JPopupMenu

 
Faisal Ul-Haque
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Bartender
Posts: 5126
11
Java Netbeans IDE Opera
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic