Mark Comix
Mark Comix

Reputation: 1898

J2ME (LWUIT) - actionPerformed Not Called

I have two LWUIT Forms (Main and Change Password)
One call the other using actionPerformed and it works.
Then, in the second one, I need to get some data, processed and return to the first one.
To do that, I'm trying to use actionPerformed again. But the Ok button (The Right Button on the Cellphone), NOT calls the actionPerformed of the Change Password Form. Just Call the actionPerformed of the Main Form. Why?

In other part of the code, I do the same, but with the MIDlet and only one Form and it works.

There is something to change?
I'm losing a lot of time testing the code, but I can't find the solution

This is the Code of the "Main" Form:

public class InfoView extends Form implements ActionListener{

    private Command backCommand;
    private Command changePasswordCommand;
    private Command okChangePasswordForm;

     private ChangePassword changePasswordForm;

    public InfoView(Command backC){

        super();

        this.addCommand(backC);
        this.setBackCommand(backC);

        this.setScrollableY(true);

        this.setTitle(LanguageManager.getText("RootTitle"));

        changePasswordCommand = new Command(LanguageManager.getText("ChangePassword"), Constants.CHANGE_PASSWORD_COMMAND);

        okChangePasswordForm = new Command(LanguageManager.getText("Ok"), Constants.OK_CHANGE_PASSWORD_COMMAND);

        this.addAllCommands();       

        this.initForm();
    }

    public void initForm(){

        Style s = this.getStyle();

        s.setMargin(0, 0, 0, 0);
        s.setPadding(0, 0, 0, 0);

        this.addCommandListener(this);

        backCommand = new Command(LanguageManager.getText("Back"), Constants.BACK_COMMAND);

    }

    private void addAllCommands(){

        this.addCommand(changePasswordCommand);

        this.addCommand(changeContactInfoCommand);
    }

    public void actionPerformed(ActionEvent arg0) {

        //Obtengo la Opción seleccionada
        Command cmd = arg0.getCommand();

        if (cmd == null) {
            return;
        }

        System.out.println(String.valueOf(cmd.getId()));

        switch (cmd.getId()) {

            case Constants.CHANGE_PASSWORD_COMMAND:

                System.out.println("CHANGE_PASSWORD_COMMAND");

                //this.setGlassPane(null);

                if (changePasswordForm == null) {
                    changePasswordForm = new ChangePassword();
                    changePasswordForm.addCommand(backCommand);
                    changePasswordForm.addCommand(okChangePasswordForm);
                    changePasswordForm.addCommandListener(this);
                    changePasswordForm.setBackCommand(backCommand);
                }

                changePasswordForm.show();
                arg0.consume();
                break;

            case Constants.BACK_COMMAND:

                System.out.println("BACK_COMMAND");

                //20111004 MB: Cuando vuelvo, desincronizo para que al 
                //cambiar de tarjeta funcione
                //protocolManager.deSync();

                addAllCommands();

                this.show();

                this.editInfoForm = null;
                this.changePasswordForm = null;

                System.gc();

                break; 

            case Constants.OK_CHANGE_PASSWORD_COMMAND:

                System.out.println("OK_CHANGE_PASSWORD_COMMAND");

                this.editInfoForm = null;

                this.show();

                System.gc();

                break;                
        }
    }
}

And this is the Code of the Change Password Form:

import com.sun.lwuit.Form;
import com.sun.lwuit.*;
import com.sun.lwuit.events.ActionEvent;
import com.sun.lwuit.events.ActionListener;
import com.sun.lwuit.layouts.BoxLayout;
import Project.language.LanguageManager;

public class ChangePassword extends Form implements ActionListener {

    private TextField oldPassword;
    private TextField newPassword;
    private TextField repeatNewPassword;

    public ChangePassword(){

        super ();

        this.setTitle(LanguageManager.getText("ChangePasswordTitle"));

        addCommandListener(this);

        this.setLayout(new BoxLayout(BoxLayout.Y_AXIS));

        this.addComponent(new Label(LanguageManager.getText("Actual")));

        oldPassword = new TextField("");
        oldPassword.setConstraint(TextArea.PASSWORD);

        this.addComponent(oldPassword);

        this.addComponent(new Label(LanguageManager.getText("New")));

        newPassword = new TextField("");
        newPassword.setConstraint(TextArea.PASSWORD);

        this.addComponent(newPassword);

        this.addComponent(new Label(LanguageManager.getText("Repeat")));

        repeatNewPassword = new TextField("");
        repeatNewPassword.setConstraint(TextArea.PASSWORD);

        this.addComponent(repeatNewPassword);
    }

    private String getOldPass(){
        return this.oldPassword.getText();
    }

    private String getNewPass(){
        return this.newPassword.getText();
    }

    public void actionPerformed(ActionEvent arg0) {

        Command cmd = arg0.getCommand();

        String oldPasswordVar = getOldPass();
        String newPasswordVar = getNewPass();

    }

}

Upvotes: 0

Views: 1165

Answers (1)

Vimal
Vimal

Reputation: 1266

Got It!

In the InfoView.actionPerformed(...), under switch Constants.CHANGE_PASSWORD_COMMAND: in the ChangePassword() constructor the ChangePassword.addCommandListener(...) is over-written by changePasswordForm.addCommandListener(this); Hence when the ChangePassword form is being displayed the keyEvents of InfoView.actionPerformed(...) are active.

Code Snippet #1

...
case Constants.CHANGE_PASSWORD_COMMAND:
    changePasswordForm = new ChangePassword();
    changePasswordForm.addCommand(backCommand);
    changePasswordForm.addCommandListener(this); //<-- PROBLEM, because 'this' 
                                                 //          is referring to
                                                 //          'InfoView' and NOT
                                                 //          'ChangePassword'
    changePasswordForm.setBackCommand(backCommand);
...

Code Snippet #2

public ChangePassword(){
 ...
 addCommandListener(this);   //<-- Being over-written
 ...
}

The solution is to just comment or remove the PROBLEM statement marked in code snippet #1 above.

I would also recommend to create backCommand inside the class ChangePassword, so that it doesn't get hidden by InfoView.backCommand.

Upvotes: 2

Related Questions