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)
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 :)
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/
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With