1 year ago

#366413

test-img

Bate

Prevent JPopupMenu from showing on right click

I have added a JPopupMenu to a JList. I use the following code:

JList list = new JList();
JPopupMenu popup = new JPopupMenu();
list.setComponentPopupMenu(popup);

Problem: the popup appears on right mouse click (which is fine), but it should only do so if the list contains items. If the list is empty and there is no item in it to be right-clicked on, the popup should not show. I have not found a way to solve this.

I know that I could - instead of using the code above - use a custom MouseListener to make the popup appear, but the given code example seems to be the suggested way to do it.

java

swing

show

jpopupmenu

0 Answers

Your Answer

Accepted video resources