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);
}
}
Was this information helpful? Send us your comments.