Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Extending JMenu to give it a check box?

I'm planning on combining JMenu and JCheckBoxMenuItem so I can have: (1) a popup menu containing instances of this new component. (2) this new component would have a check box on the left as well as being able to expand to the right and show other sub-menus (like a regular JMenu)

I had a couple questions:

First, am I reinventing the wheel? / Has this been done before?

Second, I'm trying to figure out how swing knows how to get the UI class for the extended Component class? (I see that JMenu for example has a String uiClassID member that's somehow used for that, but it's not the exact class name... I debugged it to a HashTable lookup in UIDefaults)

like image 673
Robert Avatar asked Nov 04 '22 16:11

Robert


1 Answers

  1. I think it would be confusing for the users to see a JMenu with a check box (if I understand correctly, you want to put this thing directly in a JMenuBar). I don't think this has been done very often before, and there is a reason for that :)

  2. The UI class that is used depends on the actual look and feel. See the subclasses of javax.swing.plaf.MenuItemUI

The full story about the Swing architecture is described here: http://java.sun.com/products/jfc/tsc/articles/architecture/

A look-and-feel implementation provides concrete subclasses for each abstract plaf UI class. For example, the Windows look-and-feel defines WindowsButtonUI, a WindowsScrollBarUI, and so on. When a component installs its UI delegate, it must have a way to look up the appropriate concrete class name for the current default look-and-feel dynamically. This operation is performed using a hash table in which the key is defined programmatically by the getUIClassID() method in the component. The convention is to use the plaf abstract class name for these keys.

EDIT: if you want to put this in a popup menu, note that JPopupMenu is a JComponent, therefore you can put anything there, including normal JCheckBoxes. An example is here: http://www.javarichclient.com/do-more-with-jpopupmenu/

like image 73
lbalazscs Avatar answered Nov 15 '22 00:11

lbalazscs