Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Custom JMenuItems in Java

Would it be possible to create a custom JMenuItem that contains buttons? For example would it be possible to create a JMenuITem with an item similar to this:

screenshot of Google Chrome's customize and control menu with the edit menu item circled

+----------------------------------------+
| JMenuItem [ Button | Button | Button ] |
+----------------------------------------+
like image 241
Michael Avatar asked May 12 '11 01:05

Michael


1 Answers

I doubt there is an easy way to do this. You can do something like:

JMenuItem item = new JMenuItem("Edit                       ");
item.setLayout( new FlowLayout(FlowLayout.RIGHT, 5, 0) );
JButton copy = new JButton("Copy");
copy.setMargin(new Insets(0, 2, 0, 2) );
item.add( copy );
menu.add( item );

But there are several problems:

a) the menu doesn't close when you click on the button. So that code would need to be added to your ActionListener

b) the menu item doesn't respond to key events like the left/right arrow, so there is no way to place focus on the button using the keyboard. This would involve UI changes to the menu item and I have no idea where to start for this.

I would just use the standard UI design an create sub menus.

like image 198
camickr Avatar answered Oct 30 '22 12:10

camickr