java-medatepickerlwuitmidp-2.0cldc

How to implement date picker in lwuit?


I'm developing a mobile app using j2me and lwuit.

There is a lcdui DateField (act as date picker) in j2me. Like that there is any component or item in lwuit.

How to implement the date picker (Similar to lcdui DateField) in lwuit.

The calendar (in lwuit) object is not user friendly. If phone screen size is small then it will not be correctly shown. In normal j2me (lcdui) the datefield has very good look. I want to create a component/item like that in lwuit (using lwuit in j2me).


Solution

  • You can use lcdui DateField in lwuit. I do it by this way:

    import java.util.Calendar;
    import java.util.Date;
    import javax.microedition.lcdui.CommandListener;
    import javax.microedition.lcdui.DateField;
    import javax.microedition.lcdui.Displayable;
    import javax.microedition.lcdui.Form;
    
    import com.sun.lwuit.Button;
    import com.sun.lwuit.Display;
    import com.sun.lwuit.events.ActionEvent;
    import com.sun.lwuit.events.ActionListener;
    import com.sun.lwuit.plaf.UIManager;
    
    public class DatePicker extends Button implements ActionListener {
    
      private static final String OK = "ok";
      private static final String CANCEL = "cancel";
    
      private Date date;
    
      public DatePicker() {
        setUIID("TextArea");
        addActionListener(this);
      }
    
      public Date getDate() {
        return date;
      }
    
      public void actionPerformed(ActionEvent evt) {
        final Form dateForm = new Form();
        final DateField dateField = new DateField(null, DateField.DATE);
        if (date != null) {
          dateField.setDate(date);
        }
        dateForm.append(dateField);
        final javax.microedition.lcdui.Command acceptCommand = new javax.microedition.lcdui.Command(UIManager.getInstance().localize(OK, OK),
        javax.microedition.lcdui.Command.OK, 0);
        final javax.microedition.lcdui.Command cancelCommand = new javax.microedition.lcdui.Command(UIManager.getInstance().localize(CANCEL, CANCEL), javax.microedition.lcdui.Command.CANCEL, 0);
        dateForm.addCommand(acceptCommand);
        dateForm.addCommand(cancelCommand);
        CommandListener commandListener = new CommandListener() {
          public void commandAction(javax.microedition.lcdui.Command command, Displayable displayable) { 
            if (command == acceptCommand && dateField.getDate() != null) {
              DatePicker.this.date = dateField.getDate();
              DatePicker.this.setText(DatePicker.toString(DatePicker.this.date));
            }
            Display.init(Application.getInstance().midlet); // You have to save your midlet
            Application.getInstance().mainForm.show();      // and the last lwuit Form
          }
        };
        dateForm.setCommandListener(commandListener);
        javax.microedition.lcdui.Display.getDisplay(Application.getInstance().midlet).setCurrent(dateForm);  // Application.getInstance().midlet - your j2me application midlet
    
      }      
    }
    

    So now you can use it like lwuit Component, but on the actionPerformed it will open lcdui native Form with DateField on the top.