0

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 :)

rmtheis
  • 5,992
  • 12
  • 61
  • 78
Rev
  • 2,269
  • 8
  • 45
  • 75
  • 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 Jul 02 '12 at 06:56

1 Answers1

3

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.

Community
  • 1
  • 1
gnat
  • 6,213
  • 108
  • 53
  • 73
  • gnat Thanks,`You Make Great Help`. But I have problem on event not creating canvas or displaying them.accept my apologize for bad description of problem. can you make some sample code for event part(when get key from canvas call specific event)! – Rev Jul 02 '12 at 05:08
  • @Rev just show the code that you're having problems with ([preferably in separate question](http://meta.stackexchange.com/questions/43478 "at SO, they don't like chameleon-questions")). For a sample code, check `lcdui` tag wiki, there's a reference to "MIDP event Handling" tutorial, in `EventEx3.java` _get key from canvas calls specific event_ – gnat Jul 02 '12 at 06:16
  • Thank for your help again :). I update my question(I will wait for your answer). Also I look for that sample to ensure that's solution – Rev Jul 02 '12 at 06:52
  • Gnat, Thanks again for your following my problem, and I Can't use up-vote anymore for your help (cuz I did it befor). sorry for delay. I update my question see it and I must say I checked `EventEx3.java`. In these example it use log to find if `keypress` work or not! *Is It more or I miss Something?* – Rev Jul 03 '12 at 06:04
  • @Rev that's right it uses log as you describe. Also note - log being written is just the event you're looking for, just do what you need right after `System.out.println`. Now that you're done with that simple example, you may take a look at more advanced one from _another tutorial_ I mentioned in my answer - there's same key press event triggers phone screen changes, not just writing to console – gnat Jul 04 '12 at 05:40