X

Geertjan's Blog

  • July 9, 2006

JCheckBoxMenuItem & JRadioButtonMenuItem (Part 2)

Geertjan Wielenga
Product Manager
In the comments at the end of yesterday's blog entry, Sandip referred to the Radio Buttons section in the Java Look and Feel Design Guidelines (which I didn't know about but am finding very useful). Sandip's point was that "the JRadioButtonMenuItem or JRadioButton are meant to be used in cases where it represents an exclusive choice within a set of related choices", while my example yesterday didn't represent this kind of choice (there was only one choice, which isn't really a choice). "So," I thought, "How would I go about implementing an exclusive choice (to justify not using Sandip's approach)?" The thing to use is a ButtonGroup, which is simple to implement in Matisse—just drag and drop one onto your form. But what about when we're not designing graphically, but doing everything in the code? And, secondly, we'd need two or more menu items, all subclassing CallableSystemAction. How are the JRadioButtonMenuItems in these separate classes going to share the same ButtonGroup? So, I now have this simple helper class:
public class ButtonGroupHelper {
public static ButtonGroup bg = new ButtonGroup();
/\*\* Creates a new instance of ButtonGroupHelper \*/
public ButtonGroupHelper() {
}
/\*\* Returns a ButtonGroup \*/
public static ButtonGroup returnGroup() {
return bg;
}
}

And, within the first of the two classes that subclass CallableSystemAction, I have this bit of code...

comedyChoice = new JRadioButtonMenuItem("Comedy", null);
ButtonGroup local = ButtonGroupHelper.returnGroup();
local.add(comedyChoice);

...while the second (and any subsequent classes) have the same:

romanceChoice = new JRadioButtonMenuItem("Romance", null);
ButtonGroup local = ButtonGroupHelper.returnGroup();
local.add(romanceChoice);

This lets me retrieve the same button group for each JRadioButtonMenuItem. And, this lets me implement "an exclusive choice within a set of related choices". When I select "Comedy" below, "Romance" is automatically unselected, and vice versa:

I've set it up so that when a choice is made, a related window is opened and, if the other window is open at the same time, it is closed. This means either the "Comedy" or the "Romance" window is open, but never both at the same time.

Join the discussion

Comments ( 1 )
  • Nitin Chauhan Tuesday, July 11, 2006
    Hi !
    Not exactly related to this but little annoyance if it can be corrected without investing much time
    1) menus in NetBeans 5.5 There is "too much" space on left side of dropdown ! see file/navigate menu ..view menu looks ok.
    2) find/bookmark/matching icons in editor looks like foot-steps (scatterred) unless you mouse over. they would be looking better like shift-line-left icons.
    Hope you will pass on this to GUI people of NetBeans.
    Thanks for your posts
    -Nitin
Please enter your name.Please provide a valid email address.Please enter a comment.CAPTCHA challenge response provided was incorrect. Please try again.