07 - J2ME Graphic User interface: Page 6 of 6

DateField Class

This class allows us to handle dates and times in our forms, using the java.util.Date class. The constructor is like this:

DateField (String label, int mode)

  • Mode: we can define which fields we want to edit, DATE, TIME or DATE_TIME.
package my.demo;
import java.util.Date;
import javax.microedition.lcdui.Command;
import javax.microedition.lcdui.CommandListener;
import javax.microedition.lcdui.DateField;
import javax.microedition.lcdui.Display;
import javax.microedition.lcdui.Displayable;
import javax.microedition.lcdui.Form;
import javax.microedition.midlet.MIDlet;
import javax.microedition.midlet.MIDletStateChangeException;
public class HelloWorld extends MIDlet implements CommandListener {
    private Display myDisplay;
    private Form  myForm= new Form("Form");
    private Command exitCommand = new Command("Exit", Command.EXIT,1);
    private DateField myDateField, myDate, myTime;
    public HelloWorld() {
        myDisplay = Display.getDisplay(this);
        myDateField = new DateField("DateTime", DateField.DATE_TIME);
        myDate = new DateField("Date", DateField.DATE);
        myTime = new DateField("Time", DateField.TIME);
        myDateField.setDate(new Date());
        myDate.setDate(new Date());
        myTime.setDate(new Date());
        myForm.append(myDateField);
        myForm.append(myDate);
        myForm.append(myTime);
        myForm.addCommand(exitCommand);
        myForm.setCommandListener(this);
    }
    protected void destroyApp(boolean arg0) throws MIDletStateChangeException {
        notifyDestroyed();
    }
    protected void pauseApp() {
    }
    protected void startApp() throws MIDletStateChangeException {
        myDisplay.setCurrent(myForm);
    }
    public void commandAction(Command c, Displayable d) {
        if (c==exitCommand) {
                try {
                    destroyApp(false);
                } catch (MIDletStateChangeException e) {
                    e.printStackTrace();
                }            
        }
    }
}

When executing this MIDlet, our initial screen will show something like this:

          

When editing the DateTime:

          

When editing the Date:

          

When editing the Time:

          

ChoiceGroup Class

This class allows us to display a group of options within a form. It is similar to the List class. It has two constructors:

ChoiceGroup (String label, int type)

ChoiceGroup (String label, int type, String[] elements, Image[] images)

package my.demo;
import javax.microedition.lcdui.ChoiceGroup;
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.midlet.MIDlet;
import javax.microedition.midlet.MIDletStateChangeException;
public class HelloWorld extends MIDlet implements CommandListener {
    private Display myDisplay;
    private Form  myForm= new Form("Form");
    private Form formOptionSelected;
    private Command exitCommand = new Command("Exit", Command.EXIT,1);
    private Command backCommand = new Command("Back", Command.BACK,1);
    private Command selectCommand = new Command("Select", Command.OK,1);
    private String[] options = {"Option A","Option B", "Option C"};
    private ChoiceGroup myChoiceGroup;
    public HelloWorld() {
        myDisplay = Display.getDisplay(this);
        myChoiceGroup = new ChoiceGroup("Choice group:", ChoiceGroup.MULTIPLE, options, null);
        myForm.append(myChoiceGroup);
        myForm.addCommand(exitCommand);
        myForm.addCommand(selectCommand);
        myForm.setCommandListener(this);
    }
    protected void destroyApp(boolean arg0) throws MIDletStateChangeException {
        notifyDestroyed();
    }
    protected void pauseApp() {
    }
    protected void startApp() throws MIDletStateChangeException {
        myDisplay.setCurrent(myForm);
    }
    public void commandAction(Command c, Displayable d) {
        if (c==exitCommand) {
                try {
                    destroyApp(false);
                } catch (MIDletStateChangeException e) {
                    e.printStackTrace();
                }            
        } else if (c==backCommand) {
            myDisplay.setCurrent(myForm);
        } else if (c==selectCommand) {
            String selectedOptions = "";
            boolean[] selectedItems = new boolean[myChoiceGroup.size()];
            myChoiceGroup.getSelectedFlags(selectedItems);
            for (int i=0;i<myChoiceGroup.size();i++) {
                if (selectedItems[i]) {
                    selectedOptions = selectedOptions + options[i] + "\n";
                }
            }
            formOptionSelected = new Form ("Selected Options:");
            formOptionSelected.append(selectedOptions);
            formOptionSelected.addCommand(backCommand);
            formOptionSelected.setCommandListener(this);
            myDisplay.setCurrent(formOptionSelected);
        }
    }       
}

When executing this MIDlet, our initial screen will show something like this:

          

When multiple options are selected, and select them, the following screen is shown.

          

Like us on Facebook