CodeNameOne Dynamically created Form, how to “Back”

让人想犯罪 __ 提交于 2019-12-07 01:46:34

问题


In an actionListener for a button we would like to create a Form on the fly.

Eg Something like

Button b = new Button("Clickme");
b.setActionListener(new ActionListener() {
    public void actionPerformed(ActionEvent evt) {
        Form f = new Form();
        Container c = new Container();
        ...
        f.addComponent(c);
        f.show();
    }
});

Which works fine..... but "back" button will not work

Does anyone know the correct way of implementing a dynamic form in an actionListener, or jumping to a predefined form through and action Listener?

Thanks

James


回答1:


You need to create a back command and associate it with the form:

Command back = new Command("Back") {
     public void actionPerformed(ActionEvent ev) {
         // notice that when showing a previous form it is best to use showBack() so the 
         // transition runs in reverse
         showPreviousForm();
     }
};
f.setBackCommand(back);

You can see this in the kitchen sink demo which is entirely hand coded.




回答2:


You could also giving the form as Parameter

chooseDB(c.getComponentForm());

private void chooseDB(final Form main) {
    Form f = new Form("Choose a Database");
    ...
    Command backCommand = new Command("Back") {
        public void actionPerformed(ActionEvent ev) {
            main.showBack();
        }};
    f.addCommand(backCommand);
    f.setBackCommand(backCommand);
    f.show();
}

So for your example:

Button b = new Button("Clickme");
b.setActionListener(new ActionListener() {
    public void actionPerformed(ActionEvent evt) {
        Form f = new Form();
        Container c = new Container();
        Command backCommand = new Command("Settings") {
        public void actionPerformed(ActionEvent ev) {
            b.getComponentForm().showBack();
        }};
    f.addCommand(backCommand);
    f.setBackCommand(backCommand);
        f.addComponent(c);
        f.show();
    }
});

Shai, please correct this, if i did anything wrong. Thx.



来源:https://stackoverflow.com/questions/12253698/codenameone-dynamically-created-form-how-to-back

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!