Skip to main content

Button with Command adds Forms as CommandListener

Please note these java.net forums are being decommissioned and use the new and improved forums at https://community.oracle.com/community/java.
1 reply [Last post]
handylearn
Offline
Joined: 2010-02-17

I found a problem in my code because I was not aware of the following, so I like to share it:
When I create a normal Button from a Command, with "new Button (new Command("Text", Cmdid)),"
and do all action handling in my Form, so do an button.addActionListener(form),
then the actionPerformed() method of the Form is called twice!
The button calls its action listener,
and when action is not marked as consumed,
the button calls automatically the action handling of the form it is part of.
Of course a solution is to always add ae.consumed() after you have handled the event. But because not doing it has worked so far, and because it makes the event handling a little more verbose, I had not done it.
Another solution is not to do the button.addActionListener(form). But it is sometimes not obvious when to add it and when not.
If somebody from LWUIT team reads ist, I think there should be more information about event handling in the "Lightweight UI Toolkit Developer's Guide". Also a line in the SDK docu for Button(Command) about the automatic routing of actions to the form is missing.
I use Lwuit 1.4. as it seem to be the latest official release.
Reagrds
Karsten Meier

Reply viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.
vprise
Offline
Joined: 2003-11-07

Thanks, this has changed a bit in the past to unify several different event approaches so the docs aren't as up to date.
I'm working on the docs right now so I'll try to add this as well to my HUGE todo list.