CodeNameOne Dynamically created form, like "Back",

In the actionListener for a button, we would like to create a form on the fly.

For example, 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 the back button does not work

Does anyone know the correct way to implement a dynamic form in an actionListener or go to a predefined form through an action listener?

thank

James

+5
source share
2 answers

You need to create the reverse 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 demonstration of the kitchen sink, which is fully encoded by hand.

+3
source

You can also specify the form as a 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();
    }
});

, , , - . .

0

All Articles