Question

I'm trying to build a particular JMenu. I want a JMenuItem with JMenu functionality, I.E. when we click it the item should do something (like opening a dialog). But the JMenuItem should also contain a button (or other component) that when we click it, should open a popup with a couple of options.

So, till now I have something like this:

Desired UI

That is what I have before click the arrow.

My problem is that, when I press the button (arrow), the sub-menu is actually opened, but the menu item that contains that button closes because loses focus.

Broken UI

That is the result after clicking in the arrow button.

Is there any way to manage this? Or a better way to have this behavior?

Was it helpful?

Solution 4

Couldn't actually find a solution for this particular problem. As a workaround, I used just a simple button that toggle between the option 1 and option 2, instead of having the button (arrow) that open a new popup. Thanks a lot for your help.

OTHER TIPS

We can guide you if we know exactly what you are trying to implement. If you just want to select an option, you can implement that in better way with the JRadioButtonMenuItem,so you dont really need to implement a button and then select an option.But it depends on what you really want.

That's not what a menu is intended for. Use a ribbon instead, and these things will be easy and natural.

Why not just use simple nested JMenuItem instead? Something like this (First screen from the top).

On the other hand you can benefit from a similar solution described here.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top