Create a date picker
-
Import the required classes and interfaces.
import net.rim.device.api.ui.*;
import net.rim.device.api.ui.component.*;
import net.rim.device.api.ui.container.*;
import net.rim.device.api.ui.picker.*;
import java.util.*;
-
Create the application framework by extending the
UiApplication class. In
main(), create an instance of the new class and
invoke
enterEventDispatcher() to enable the application to
receive events. In the constructor, invoke
pushScreen() to display the custom screen for the
application. The
DatePickScreen class represents the custom screen
that is described in step 3.
public class DatePick extends UiApplication
{
public static void main(String[] args)
{
DatePick theApp = new DatePick();
theApp.enterEventDispatcher();
}
public DatePick()
{
pushScreen(new DatePickScreen());
}
}
-
Create the custom screen for the application by extending the
MainScreen class. In the constructor, invoke
setTitle() to display a title on the screen. Invoke
add() to display a rich text field on the screen.
class DatePickScreen extends MainScreen
{
public DatePickScreen()
{
setTitle("Date Picker Sample");
add(new RichTextField("Trying Date Picker"));
}
}
-
Add a section of code to the event queue of the application by
invoking
invokeLater(). Create a
Runnable object and pass it as a parameter to
invokeLater(). Override
run() in the definition of
Runnable.
class DatePickScreen extends MainScreen
{
public DatePickScreen()
{
setTitle("Date Picker Sample");
add(new RichTextField("Trying Date Picker"));
UiApplication.getUiApplication().invokeLater(new Runnable()
{
public void run()
{
}
});
}
}
-
In
run(), invoke
DateTimePicker.getInstance() to return a
DateTimePicker object. Invoke
doModal() to display the date picker. Invoke
getDateTime() to return a
Calendar object that represents the date and time
the user selects. Use
getTime() to return the date and time as a
Date object. Use
Dialog.alert() to display the selected date and
time.
class DatePickScreen extends MainScreen
{
public DatePickScreen()
{
setTitle("Date Picker Sample");
add(new RichTextField("Trying Date Picker"));
UiApplication.getUiApplication().invokeLater(new Runnable()
{
public void run()
{
DateTimePicker datePicker = DateTimePicker.getInstance();
datePicker.doModal();
Calendar cal = datePicker.getDateTime();
Date date = cal.getTime();
Dialog.alert("You selected " + date.toString());
}
});
}
}
Code sample: Creating a date picker
import net.rim.device.api.ui.*;
import net.rim.device.api.ui.picker.*;
import net.rim.device.api.ui.component.*;
import net.rim.device.api.ui.container.*;
import net.rim.device.api.database.*;
import net.rim.device.api.io.*;
import java.util.*;
public class DatePick extends UiApplication
{
public static void main(String[] args)
{
DatePick theApp = new DatePick();
theApp.enterEventDispatcher();
}
public DatePick()
{
pushScreen(new DatePickScreen());
}
}
class DatePickScreen extends MainScreen
{
public DatePickScreen()
{
setTitle("Date Picker Sample");
add(new RichTextField("Trying Date Picker"));
UiApplication.getUiApplication().invokeLater(new Runnable()
{
public void run()
{
DateTimePicker datePicker = DateTimePicker.getInstance();
datePicker.doModal();
Calendar cal = datePicker.getDateTime();
Date date = cal.getTime();
Dialog.alert("You selected " + date.toString());
}
});
}
}
Was this information helpful? Send us your comments.