0

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?

Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
Luis Gonçalves
  • 249
  • 2
  • 10
  • 1
    So you want one menu item (one line in the menu) to have two different behaviors - perform some action when clicking on the text and open a submenu when clicking on the button? That would not be very userfriendly, better use a menugroup for the submenu, i.e. ad a new (sub) `JMenu`. – Thomas Oct 19 '11 at 14:24
  • my question, why did you put JButton in the JMenu as JMenuItem, are you not satisfied with the basic implementations – mKorbel Oct 19 '11 at 14:25
  • @Thomas: Yes, that's exactly what I want: perform an action when click in the text and open the popup when click the button. – Luis Gonçalves Oct 19 '11 at 14:39
  • 2
    I'm not entirely convinced this is something you *should* do. Having said that: For better help sooner, post an [SSCCE](http://pscode.org/sscce.html). – Andrew Thompson Oct 19 '11 at 14:49
  • `JButton m = new JButton();` `JMenuBar abc = (JMenuBar) new JMenuBar().add(m);` How about something like this? – COD3BOY Oct 19 '11 at 15:02
  • So, I spent some time looking at this and I realized that the MenuItem with the arrow button closes because a new popup is opened and not related with the first. I solved this problem just adding a button that toggle between the option 1 and option 2 and in that case I avoid the second popup and all the problems related with it. Thanks for your help. – Luis Gonçalves Oct 24 '11 at 07:55

4 Answers4

2

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.

COD3BOY
  • 11,964
  • 1
  • 38
  • 56
2

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

Ingo Kegel
  • 46,523
  • 10
  • 71
  • 102
  • 1
    I agree, using a menu like that would be quite counter-intuitive. Ribbons seem like the way better alternative. – Thomas Oct 19 '11 at 14:42
1

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.

Community
  • 1
  • 1
Wojciech Owczarczyk
  • 5,595
  • 2
  • 33
  • 55
0

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.

Luis Gonçalves
  • 249
  • 2
  • 10