Right Click on JComboBox [Java Swing component]

Recently I was writing a small UI tool in Java Swing at work and found a strange bug in Java Swing, which was first reported in 1998 but is not fixed yet. I thought of documenting the workaround here so that it is useful for others who are faced with the same problem.

I wanted to trigger a popup menu when a right click event happens on a JComboBox (drop-down) component. I tried adding a MouseListener to the JComboBox, but it was not getting triggered. After spending a couple of hours, I finally came to know that it was because of a bug in Java Swing. The worst part is that the bug was closed as “Will not fix”

Fortunately, there is a workaround for this. Instead of adding the MouseListener to the JComboBox, you have to add it to all its children, like how it is done in the following code snippet.

After implementing this fix, I was able to trigger the popup when the combo box was right-clicked. Hope this helps someone who is affected by this bug.

Related posts

Tags: ,

3 Comments so far

Follow up comments through RSS Feed | Post a comment

1 Tweetbacks so far

1 Trackbacks/Pingbacks so far

Leave a Reply

Your email address will not be published. Required fields are marked *