Programming Tutorials

DateField sample program in J2ME

By: Jagan in J2ME Tutorials on 2007-09-16  

Here's an example program that demonstrates the use of DateField in a J2ME application:

import javax.microedition.midlet.*;
import javax.microedition.lcdui.*;
import java.util.*;

public class DateFieldExample extends MIDlet implements CommandListener {

    private Form form;
    private DateField dateField;
    private Command exitCommand;

    public DateFieldExample() {
        form = new Form("DateField Example");
        dateField = new DateField("Select a date:", DateField.DATE_TIME);
        exitCommand = new Command("Exit", Command.EXIT, 0);
        form.append(dateField);
        form.addCommand(exitCommand);
        form.setCommandListener(this);
    }

    public void startApp() {
        Display.getDisplay(this).setCurrent(form);
    }

    public void pauseApp() {}

    public void destroyApp(boolean unconditional) {}

    public void commandAction(Command c, Displayable d) {
        if (c == exitCommand) {
            notifyDestroyed();
        }
    }
}

In this example, we create a DateField and add it to a Form. The DateField is set to display both the date and time. We also create an Exit command and add it to the Form. Finally, we set the CommandListener to handle the Exit command.

When the user selects a date using the DateField, the selected date and time will be displayed in the DateField.

Note that in order to use DateField in a J2ME application, you need to import the javax.microedition.lcdui.DateField class.






Add Comment

* Required information
1000

Comments

No comments yet. Be the first!

Most Viewed Articles (in J2ME )

Latest Articles (in J2ME)