|
Replies:
2
-
Last Post:
Jun 24, 2009 8:10 AM
by: a_schroeder
|
|
|
|
|
|
|
Deriving components breaks ActionListener?
Posted:
Jun 23, 2009 9:15 AM
|
|
|
Hello All,
I've encountered a problem with ActionListener that has me scratching my head: I'm deriving from Form since I want to keep some of the data operations inside the Form (I'm currently porting an older project using J2ME to LWUIT, the data-crunching in the background is a bit...errm...hairy so I'd like to touch it as little as possible). The problem is: When I create an ActionListener for my derived class and use setCommandListener on an instance of the class, the ActionListener is never triggered when I press the softkeys. It doesn't matter if I have my derived class implement ActionListener itself, or pass an external ActionListener obejct (like for instance having the main application implement the ActionListener and passing it to the instance of my derived class). This doesn't only happen for forms, it also affects Buttons for example. In order to see exactly what I mean, try the following:
- Create a Form: Form f=new Form(); - Add a Button to the Form: Button b = new Button; b.setText("Button 1"); f.addComponent(b); - Give the Button an ActionListener, for example: b.addActionListener(new ActionListener(){public void actionPerformed(ActionEvent ae){b.setText("it works");}}); - Create a new class "Mybutton" derived from Button that implements ActionListener with the same implementation of actionPerformed(): public class Mybutton extends Button implements ActionListener{ public void actionPerformed(ActionEvent ae){ this.setText("it works"); } } - Add an instance of "Mybutton" to the form: Mybutton m = new Mybutton(); m.setText=("Button 2"); f.addComponent(m); - Show the form: f.show(); and click both buttons. The standard Button will change its Text, while the Mybutton will not.
You will notice that you can set a breakpoint in m's actionPerformed(), but it will never be reached, so I assume this actionPerformed() is never called.
Strangely, when you give your instance of Mybutton an ActionListener by hand using m.addActionListener(new ActionListener(){b.setText("it works")}); it does work.
However, when deriving from Form, I found that I could not use setCommandListener() properly, no matter what parameter I gave it, pressing the softkeys on an instance of my derived "Meta-Form" didn't have any effect and again using the debugger shows, that actionPerformed() isn't called at all, no matter if I use an external handler object that implements ActionListener, have my derived class implement ActionListener, nor have the main App handle the ActionListener business.
Is this supposed to be that way? Am I catastrophically overlooking something?
Regards from Germany Andreas
fixed a little typo that might have caused confusion
Message was edited by: a_schroeder
|
|
|
|
|
|
|
Re: Deriving components breaks ActionListener?
Posted:
Jun 24, 2009 5:10 AM
in response to: a_schroeder
|
|
|
Have you tried adding an action listener to your button called m?
If yes, try posting the code as is, without the running commentary, I might be able to see better what you have coded.
|
|
|
|
|
|
|
|
Re: Deriving components breaks ActionListener?
Posted:
Jun 24, 2009 8:05 AM
in response to: thisisnotme
|
|
|
Thank you, I found the problem. It was part homemade (having to do with the legacy code I've got to work with, there's an innocuous empty "keyReleased" function there, that's why no actionEvents were dispatched whatsoever) and part my stupidity, as I didn't remember to set the ActionListener correctly (what I wanted to do should include the line
this.setActionListener(this)
in the constructor of my derived button class, so I wouldn't have to set the listeners "manually" later).
This is only related to the purpose of demoing the problem however, as I felt that it had something to do with deriving component classes, which was obviously a wrong assumption on my part, my failure to set the listener correctly simply made it look like a problem with derived components. Sorry for the confusion.
Regards from Germany Andreas
Message was edited by: a_schroeder
|
|
|
|
|