radio button groups

12 views
Skip to first unread message

Ian Martins

unread,
Apr 21, 2010, 5:28:32 PM4/21/10
to SDL/Swing
what is the best way to specify button groups for
JRadioButtonMenuItems? can it be done within sdl, or should each
menuitem be given an ID, then looked up and added to a group in the
initialize?

-Ian


--
Subscription settings: http://groups.google.com/group/sdlswing/subscribe?hl=en

Dan Leuck

unread,
Apr 21, 2010, 6:23:54 PM4/21/10
to sdls...@googlegroups.com
Hi Ian,

SDL/Swing makes this especially easy:

SDL menu definition:

menubar {
"Colors" ID="colors" {
radios {
"Red"
"Green"
"Blue"
}
}
}

The SDL/Swing code above creates a menubar with a menu called "Colors"
and three JRadioButtonMenuItems that are automatically grouped. You
can access the group from Java like so:

ButtonGroup group =
form.getChildByID("colors").getItem(0).getModel().getGroup();

Dan

Ian Martins

unread,
Apr 22, 2010, 9:33:09 AM4/22/10
to sdls...@googlegroups.com
very nice. the radio groups work.

the getChildByID returns null, though. I think it's because the menu is
outside of the form. if I put it inside the form I get a blank window.
any ideas?
also, is there a difference between menubar and menus?

-Ian

Dan Leuck

unread,
Apr 22, 2010, 2:48:50 PM4/22/10
to sdls...@googlegroups.com
Hi Ian,

> very nice. the radio groups work.

Great!

> the getChildByID returns null, though. I think it's because the
menu is outside of the form.

Actually, that's a bug, which has been corrected in the current
unreleased codebase. The top level menu is a special case that is
associated with the form set on the frame's content pane. getChildByID
currently returns null if you put IDs on menu items (i.e. it works for
menus but not menu items.) We'll do a release today that includes this
fix as well as an API enhancement that lets you get a group with:

form.getGroup("colors");

> if I put it inside the form I get a blank window. any ideas?

If you post your SDL, I'm happy to take a look. Note that your problem
probably relates to the menu item bug mentioned above, which we will
correct today.

> also, is there a difference between menubar and menus?

Yes. A menu bar contains menus.

JMenuBar: http://java.sun.com/javase/6/docs/api/javax/swing/JMenuBar.html

A menu is added to a menubar and contains menu items.

JMenu: http://java.sun.com/javase/6/docs/api/javax/swing/JMenu.html

Dan

Ian Martins

unread,
Apr 22, 2010, 3:12:10 PM4/22/10
to sdls...@googlegroups.com
Dan Leuck wrote:
> Hi Ian,
>
>
>> very nice. the radio groups work.
>>
>
> Great!
>
> > the getChildByID returns null, though. I think it's because the
> menu is outside of the form.
>
> Actually, that's a bug, which has been corrected in the current
> unreleased codebase. The top level menu is a special case that is
> associated with the form set on the frame's content pane. getChildByID
> currently returns null if you put IDs on menu items (i.e. it works for
> menus but not menu items.) We'll do a release today that includes this
> fix as well as an API enhancement that lets you get a group with:
>
> form.getGroup("colors");
>
>
>> if I put it inside the form I get a blank window. any ideas?
>>
>
> If you post your SDL, I'm happy to take a look. Note that your problem
> probably relates to the menu item bug mentioned above, which we will
> correct today.
>
thanks, but I'm sure my problem is what you described above. I was
trying to get menu items. I'll let you know if the update fixes it.

Dan Leuck

unread,
Apr 22, 2010, 7:35:17 PM4/22/10
to sdls...@googlegroups.com
>> If you post your SDL, I'm happy to take a look. Note that your problem
>> probably relates to the menu item bug mentioned above, which we will
>> correct today.
>>
>
> thanks, but I'm sure my problem is what you described above.  I was trying
> to get menu items.  I'll let you know if the update fixes it.


The update, which will be posted this evening, fixes the menu item
access bug. We've verified it in testing.

Best,
Dan

Ian Martins

unread,
Apr 23, 2010, 3:40:37 PM4/23/10
to sdls...@googlegroups.com
Dan Leuck wrote:
>>> If you post your SDL, I'm happy to take a look. Note that your problem
>>> probably relates to the menu item bug mentioned above, which we will
>>> correct today.
>>>
>>>
>> thanks, but I'm sure my problem is what you described above. I was trying
>> to get menu items. I'll let you know if the update fixes it.
>>
>
>
> The update, which will be posted this evening, fixes the menu item
> access bug. We've verified it in testing.
>
> Best,
> Dan
>
Yes, it is fixed with the .91 beta. Thanks

It's not a problem but one note: getChildByID only works for menuitems
in "shown" but works for form items in "initialize" and "shown."

Dan Leuck

unread,
Apr 23, 2010, 4:04:49 PM4/23/10
to sdls...@googlegroups.com
> Yes, it is fixed with the .91 beta.  Thanks

My pleasure.

> It's not a problem but one note: getChildByID only works for menuitems in
> "shown" but works for form items in "initialize" and "shown."

When I update the docs this weekend, the reason for this will be
clear. For the top level form (the one set as the content pane for the
frame), we have to do some special handling because the menu isn't
actually contained by the form in the component hierarchy. We do some
special wiring to make things like getChildByID work. Its important
that the menu actually be set on the JFrame via setJMenuBar to ensure
the keybindings work correctly and because some Look and Feel
implementations behave strangely if the menu isn't installed in this
manner. The framework takes care of doing this for you. From an SDL /
Swing perspective, we treat the menu as a child of the form. Our goal
is to abstract these details and make things as simple as possible.

Dan
Reply all
Reply to author
Forward
0 new messages