Question

I attempt to make a simple "hello world" application, where on clicking the button , it prints a string "hello world". How can I add a button on a form?

I need to create a button, on which when I click it can produce a string. How can I add a button without using canvas in j2me?

Was it helpful?

Solution

There is an API for this, but you better think twice whether you really need it.

API is desctibed in the Appearance modes section for lcdui Item objects

The StringItem and ImageItem classes have an appearance mode attribute that can be set in their constructors. This attribute can have one of the values PLAIN, HYPERLINK, or BUTTON. An appearance mode of PLAIN is typically used for non-interactive display of textual or graphical material. The appearance mode values do not have any side effects on the interactivity of the item. In order to be interactive, the item must have one or more Commands (preferably with a default command assigned), and it must have a CommandListener that receives notification of Command invocations...

A StringItem or ImageItem in BUTTON mode can be used to create a button-based user interface...

Note that this section also explains cases when using button appearance might be problematic:

...This can easily lead to applications that are inconvenient to use. For example, in a traversal-based system, users must navigate to a button before they can invoke any commands on it. If buttons are spread across a long Form, users may be required to perform a considerable amount of navigation in order to discover all the available commands. Furthermore, invoking a command from a button at the other end of the Form can be quite cumbersome. Traversal-based systems often provide a means of invoking commands from anywhere (such as from a menu), without the need to traverse to a particular item. Instead of adding a command to a button and placing that button into a Form, it would often be more appropriate and convenient for users if that command were added directly to the Form. Buttons should be used only in cases where direct user interaction with the item's string or image contents is essential to the user's understanding of the commands that can be invoked from that item.

OTHER TIPS

From the class diagram I found in an old J2ME book, and which is online at http://www.stardeveloper.com/articles/display.html?article=2002121101&page=2 it seems that J2ME don't do buttons. Well no need for them on an old mobile phone.

Just create a "hello" command and add it to a menu or form. The system will then put it on whatever button is available on your device. For touch screen devices that probably turns it into something clickable.

Here's the code

import javax.microedition.lcdui.Command;
import javax.microedition.lcdui.CommandListener;
import javax.microedition.lcdui.Display;
import javax.microedition.lcdui.Displayable;
import javax.microedition.lcdui.Form;
import javax.microedition.lcdui.TextBox;
import javax.microedition.lcdui.TextField;
import javax.microedition.midlet.MIDlet;
import javax.microedition.midlet.MIDletStateChangeException;


public class HelloWorld extends MIDlet implements CommandListener {

    private static final String HELLO_WORLD = "Hello, World!!";

    private Form form= new Form ("");

    private Command exit= new Command("Exit", Command.EXIT, 0x01);
    private Command ok= new Command("OK", Command.OK, 0x01);
    private Command hello= new Command("HELLO", Command.SCREEN, 0x01);

    private TextBox textBox= new TextBox("Hello World", HELLO_WORLD, HELLO_WORLD.length(), TextField.UNEDITABLE);

    public HelloWorld() {
        this.form.addCommand(exit);
        this.form.addCommand(hello);
        this.form.setCommandListener(this);
        this.textBox.addCommand(ok);
        this.textBox.addCommand(exit);
        this.textBox.setCommandListener(this);
    }

    protected void destroyApp(boolean unconditional)
            throws MIDletStateChangeException { }

    protected void pauseApp() { }

    protected void startApp() throws MIDletStateChangeException {
        Display.getDisplay(this).setCurrent(this.form);
    }

    public void commandAction(Command c, Displayable d) {
        if (c == this.exit) {
            this.notifyDestroyed();
        }
        if(c == this.ok) {
            Display.getDisplay(this).setCurrent(this.form);         
        }
        if(c == this.hello) {
            Display.getDisplay(this).setCurrent(this.textBox);          
        }
    }

}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top