0
votes

I'm a dot net developer. And recently forced to do something in j2me.

We have app in j2me for working with SMS-Text-Message and make different UI based on these sms. In this app form create manually with Canvas.

There are several operations (which executable via selection of different options by user) in each canvas. Some of these operation create new canvas (something like multi Form show ).

Is there solution to define event in each canvas form and then after calling so some operation (like create new canvas).

More Info:

For Example I have blow Code (Canvas-Form):

public void keyPressed(int key) {
        if (key == -3) {
            // call OK-event 
        }
    }

It means when user input specific key like, event call to back to base midlet.(in this case Canvas-Form and midlet are in different java classes).

To do this in Dot net, We define event in Canvas-Form, then call it. Also we handle that event in midlet-class and write own code int that handle-method

So my Question Is How do same thing's in J2me?

More and More Additional Info(Update 2)

My knowledge about java and j2me is less than Alga knowledge about this:). So maybe my question seems ridiculous. But my Question has these parts:

1) Define Event (I don't know how!)
2) Call Event (where I write call OK-event comment in code sample)
3) Handle Event Method (I don't know how!)

I my search, I see a lot of examples how to define event with command. But in canvas form should I define Command to do this or, no need to Command because I draw buttons in canvas. I hope someone can understand my problem with this description.

And hope it prevent Downvotes :)

1
your code example lacks logging: right above if (key == -3) add something like System.out.println("keyPressed [" + key + "]") and re-run your test (look into emulator console while testing, key pressed messages will go there) - gnat

1 Answers

3
votes

This is quite easy to do. Canvas can listen to key press and pointer events, as well as to commands. You implement the operations you need in respective methods defined in the API.

If you're interested, find more details on that in Canvas API documentation.

To create new canvas is also easy since these are plain old Java objects, no magic.You seem to be mostly active in C#, expect it to be pretty much like you create instances of C# objects.

The only specifics worth remembering is that to make your canvas (or any Displayable for that matter) visible, you need an instance of Display that corresponds to your application. The only way to obtain that instance is from the class that extends MIDlet - from the class that serves as an entry point to your MIDP application.

You have to get the Display instance there and further make sure that it's available anywhere you need it. That instance is also a plain Java object, pretty similar to C# object and the ways to expose it are nothing MIDP-specific.

If you need to learn more details, consider also studying references to tutorials and API documentation at


For the code snippet provided in question update, the way to find out what's going on there would be to add appropriate logging and re-test it in emulator, looking into emulator console when you press keys.

public void keyPressed(int key) {
        // add logging here:
        System.out.println("keyPressed [" + key + "]");
        if (key == -3) {
            // add logging here:
            System.out.println("calling OK-event");
            // call OK-event 
        }
}

For a sample code, check lcdui tag wiki, there's a reference to "MIDP event Handling" tutorial, in EventEx3.java. Another tutorial listed in lcdui tag wiki worth looking at is "J2ME Tutorial: User Interfaces with MIDP 2.0", section Working with the Low-Level API - there is sample code as well.