Domanda

Here's what I want to do. I am making a popup that suggests words, 10 at a time, with the option to display the next 10 suggestions. I designed this as a JPopupMenu with 11 items; 10 suggestions and one "Show more..." option.

enter image description here

The code that is run on the "Show more..." removes all the previous items, and adds the new ones (simple enough).

The problem is that when I press "Show more..." the JPopupMenu closes (as I suppose is the default behaviour). I have tried working my way around this, even by creating a whole new popup and display it on the same location.

Is there a simple way to make a JPopupMenu NOT disappear when pressing one of its items? Thanks in advance!

È stato utile?

Soluzione

I've been using this for checkbox items:

import javax.swing.JCheckBoxMenuItem;
import javax.swing.JComponent;
import javax.swing.MenuSelectionManager;
import javax.swing.plaf.ComponentUI;
import javax.swing.plaf.basic.BasicCheckBoxMenuItemUI;

public class StayOpenCheckBoxMenuItemUI extends BasicCheckBoxMenuItemUI {

    @Override
    protected void doClick(MenuSelectionManager msm) {
        menuItem.doClick(0);
    }

    public static ComponentUI createUI(JComponent c) {
        return new StayOpenCheckBoxMenuItemUI();
    }

}

Then, register it:

myJCheckBoxMenuItem.setUI(new StayOpenCheckBoxMenuItemUI());

Depending on what type of menu item your 'Show more' item is, similar solution should work. Just subclass the proper UI instead of BasicCheckBoxMenuItemUI.

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top