user2978508
user2978508

Reputation: 21

How to pass control from GUI created form to code created form in codenameONe

I have created a form using GUI builder and another form via code which loads the google map. There is a button in the form which was created by using GUI builder, I need to go to the map while clicking the button. How to do it ? please help.

Upvotes: 2

Views: 366

Answers (2)

Diamond
Diamond

Reputation: 7483

Add an actionListener to your button and call the new form in the method generated in StateMachine.

Your form should be created in StateMachine.

For example:

protected void onPage2_ButtonAction(Component c, ActionEvent event) {
    Form hi = new GmapForm("Native Maps Test");
    hi.show();
}

Gmap Form Class:

public class GmapForm extends Form {

 public GmapForm(String title) {
    t.setTitle(title);
    setLayout(new BorderLayout());
    final MapContainer cnt = new MapContainer();
    addComponent(BorderLayout.CENTER, cnt);
    addCommand(new Command("Move Camera") {
        public void actionPerformed(ActionEvent ev) {
            cnt.setCameraPosition(new Coord(-33.867, 151.206));
        }
    });
    addCommand(new Command("Add Marker") {
        public void actionPerformed(ActionEvent ev) {

            System.out.println("Marker");

            try {
                cnt.setCameraPosition(new Coord(41.889, -87.622));
                cnt.addMarker(EncodedImage.create("/maps-pin.png"), new Coord(41.889, -87.622), "Hi marker", "Optional long description", new ActionListener() {
                    public void actionPerformed(ActionEvent evt) {
                        Dialog.show("Marker Clicked!", "You clicked the marker", "OK", null);
                    }
                });

            } catch(IOException err) {
                // since the image is iin the jar this is unlikely
                err.printStackTrace();
            }

        }
    });
    addCommand(new Command("Add Path") {
        public void actionPerformed(ActionEvent ev) {
            cnt.setCameraPosition(new Coord(-18.142, 178.431));
            cnt.addPath(new Coord(-33.866, 151.195), // Sydney
                new Coord(-18.142, 178.431),  // Fiji
                new Coord(21.291, -157.821),  // Hawaii
                new Coord(37.423, -122.091)  // Mountain View
            );
        }
    });
    addCommand(new Command("Clear All") {
        public void actionPerformed(ActionEvent ev) {
            cnt.clearMapLayers();
        }
    });
    revalidate();
  }

Remove the above code from your Gmap class and Uncomment // new StateMachine("/theme");

Upvotes: 1

user2978508
user2978508

Reputation: 21

// Map loading class

import com.codename1.io.Log;
import com.codename1.ui.events.ActionEvent;
import com.codename1.ui.events.ActionListener;
import com.codename1.ui.Display;
import com.codename1.ui.Form;
import userclasses.StateMachine;
import com.codename1.maps.Coord;
import com.codename1.ui.Command;
import com.codename1.ui.Dialog;
import com.codename1.ui.EncodedImage;
import com.codename1.ui.layouts.BorderLayout;
import com.codename1.ui.plaf.UIManager;
import com.codename1.ui.util.Resources;
import java.io.IOException;

public class Gmap {

    private Form current;

    public void init(Object context) {
        // Pro only feature, uncomment if you have a pro subscription
        // Log.bindCrashProtection(true);
        try {
            Resources theme = Resources.openLayered("/theme");
            UIManager.getInstance().setThemeProps(theme.getTheme(theme.getThemeResourceNames()[0]));
        } catch(IOException e){
            e.printStackTrace();
        }
    }

    public void start() {
        if(current != null){
            current.show();
            return;
        }
       // new StateMachine("/theme"); 
        Form hi = new Form("Native Maps Test");
        hi.setLayout(new BorderLayout());
        final MapContainer cnt = new MapContainer();
        hi.addComponent(BorderLayout.CENTER, cnt);
        hi.addCommand(new Command("Move Camera") {
            public void actionPerformed(ActionEvent ev) {
                cnt.setCameraPosition(new Coord(-33.867, 151.206));
            }
        });
        hi.addCommand(new Command("Add Marker") {
            public void actionPerformed(ActionEvent ev) {

                System.out.println("Marker");

                try {
                    cnt.setCameraPosition(new Coord(41.889, -87.622));
                    cnt.addMarker(EncodedImage.create("/maps-pin.png"), new Coord(41.889, -87.622), "Hi marker", "Optional long description", new ActionListener() {
                        public void actionPerformed(ActionEvent evt) {
                            Dialog.show("Marker Clicked!", "You clicked the marker", "OK", null);
                        }
                    });

                } catch(IOException err) {
                    // since the image is iin the jar this is unlikely
                    err.printStackTrace();
                }

            }
        });
        hi.addCommand(new Command("Add Path") {
            public void actionPerformed(ActionEvent ev) {
                cnt.setCameraPosition(new Coord(-18.142, 178.431));
                cnt.addPath(new Coord(-33.866, 151.195), // Sydney
                    new Coord(-18.142, 178.431),  // Fiji
                    new Coord(21.291, -157.821),  // Hawaii
                    new Coord(37.423, -122.091)  // Mountain View
                );
            }
        });
        hi.addCommand(new Command("Clear All") {
            public void actionPerformed(ActionEvent ev) {
                cnt.clearMapLayers();
            }
        });

        hi.show();
    }

    public void stop() {
        current = Display.getInstance().getCurrent();
    }

    public void destroy() {
    }
}

// action for the button which I have created by using GUI builder

protected void onPage2_ButtonAction(Component c, ActionEvent event) {

// Here I want to redirect to the map page

}

Upvotes: 0

Related Questions