5

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)

Robert
  • 247
  • 4
  • 12
  • 1
    See also [*How to Use Menus*](http://docs.oracle.com/javase/tutorial/uiswing/components/menu.html). – trashgod Sep 06 '12 at 20:59
  • 1
    no idea what did you talking about, for better help sooner post an [SSCCE](http://sscce.org/) with detailed descriptions about your 4-5 issues – mKorbel Sep 06 '12 at 21:08
  • 1
    What would a checkbox at that level represent? If all of the JMenuItems were checkboxes, and it signified that they were all selected then I could see it, but otherwise I have no idea. – BillRobertson42 Sep 06 '12 at 21:27
  • Bill, the JMenuItem's checkbox is supposed to enable/disable the checkboxes of its JCheckBoxMenuItem children. – Robert Sep 06 '12 at 21:50

1 Answers1

2
  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/

lbalazscs
  • 17,474
  • 7
  • 42
  • 50
  • For (1) I actually want to (was told to) put it in a popup menu instead of a JMenuBar. Ok, different look and feel options makes sense. Thanks, I'll take a look at the link. – Robert Sep 06 '12 at 21:47
  • I edited my answer to add one more idea, which is probably easier to implement than messing with the UI delegates. – lbalazscs Sep 06 '12 at 22:01
  • I am also in search of such implementation.@user1399747 Did you find the way..? – NIKUNJ SANGHADIA Nov 12 '13 at 05:14