Development Guide
Local Navigation
- UI
- Fundamental UI components
- Drawing directly on a screen
- UI life cycle
-
UI components
- Add a UI component to a screen
- Aligning a field to a line of text
- Buttons
- Check boxes
- Create a bitmap
- Create a custom field
- Creating a field to display web content
- Dialog boxes
- Drop-down lists
- Labels
- Lists and tables
- Radio buttons
- Activity indicators and progress indicators
- Pickers
- Search
- Spin boxes
- Text fields
- Tree views
- Events
- Command Framework API
- Arranging UI components
- Advanced UI components and managers
- Menu items
- Fonts
- Spelling checker
- Barcode scanning
- BlackBerry device user input and navigation
- Navigation events
- Accelerometer
- Magnetometer
- Tutorial: Creating a custom button
- Related resources
- Glossary
- Provide feedback
- Document revision history
- Legal notice
BlackBerry Manuals & Help
>
Developer Documentation
>
Java Development Guides and API Reference
>
Development Guide
UI and Navigation - BlackBerry Java SDK - 7.0
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()); } }); } }
Create a file picker
-
Import the required classes and interfaces.
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 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 application constructor, invoke
pushScreen() to display the custom screen for the
application. The
FilePickScreen class represents the custom screen
that is described in step 3.
public class FilePick extends UiApplication { public static void main(String[] args) { FilePick theApp = new FilePick(); theApp.enterEventDispatcher(); } public FilePick() { pushScreen(new FilePickScreen()); } } -
Create the custom screen for the application by extending the
MainScreen class. In the screen constructor, invoke
setTitle() to specify a title for the screen. Invoke
add() to add a label field to the screen.
class FilePickScreen extends MainScreen { public FilePickScreen() { setTitle("File Picker Sample"); add(new LabelField("Trying File Picker")); } } -
In the screen constructor, invoke
invokeLater() to add a section of code to the event
queue of the application. Create a
Runnable object and pass it as a parameter to
invokeLater().
class FilePickScreen extends MainScreen { public FilePickScreen() { setTitle("File Picker Sample"); add(new LabelField("Trying File Picker")); UiApplication.getUiApplication().invokeLater(new Runnable() { public void run() { } }); } } -
Override
run() in the definition of
Runnable. In
run(), invoke
FilePicker.getInstance() to return a
FilePicker object. Create a
FilePickListener object, which is described in step
6. Invoke
setListener() to register the listener for the file
picker. Invoke
show() to display the file picker on the screen.
class FilePickScreen extends MainScreen { public FilePickScreen() { setTitle("File Picker Sample"); add(new LabelField("Trying File Picker")); UiApplication.getUiApplication().invokeLater(new Runnable() { public void run() { FilePicker fp = FilePicker.getInstance(); FilePickListener fileListener = new FilePickListener(); fp.setListener(fileListener); fp.show(); } }); } } -
Invoke
Dialog.alert to create a dialog box with a message
that displays which file was selected. Invoke
Dialog.alert in a class that implements the
FilePicker.Listener interface by overriding
selectionDone(). The application calls
selectionDone() when the user selects a file using
the file picker.
class FilePickListener implements FilePicker.Listener { public void selectionDone(String str) { Dialog.alert("You selected " + str); } }
Code sample: Creating a file 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.io.*;
public class FilePick extends UiApplication
{
public static void main(String[] args)
{
FilePick theApp = new FilePick();
theApp.enterEventDispatcher();
}
public FilePick()
{
pushScreen(new FilePickScreen());
}
}
class FilePickScreen extends MainScreen
{
public FilePickScreen()
{
setTitle("File Picker Sample");
add(new LabelField("Trying File Picker"));
UiApplication.getUiApplication().invokeLater(new Runnable()
{
public void run()
{
FilePicker fp = FilePicker.getInstance();
FilePickListener fileListener = new FilePickListener();
fp.setListener(fileListener);
fp.show();
}
});
}
}
class FilePickListener implements FilePicker.Listener
{
public void selectionDone(String str)
{
Dialog.alert("You selected " + str);
}
}
Next topic:
Search
Previous topic: Best practice: Implementing pickers