Help Center
Local Navigation
- Creating a UI that is consistent with standard BlackBerry UIs
- BlackBerry device user input and navigation
- Screens
- Accelerometer
- Events
- Command Framework API
- Arranging UI components
-
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
- Images
- Menu items
- Custom fonts
- Spelling checker
- 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 - 6.0
Create a radio button
You can create a
group of radio buttons by using the
RadioButtonGroup class. A user can select only one
option from a group of radio buttons.
-
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.*;
-
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
MyUiScreen class, which is described in step 3,
represents the custom screen.
public class MyUi extends UiApplication { public static void main(String[] args) { MyUi theApp = new MyUi(); theApp.enterEventDispatcher(); } public MyUi() { pushScreen(new MyUiScreen()); } } -
Create the custom screen for the application by extending the
MainScreen class. In the screen constructor, invoke
setTitle() to specify the title for the screen.
class MyUiScreen extends MainScreen { public MyUiScreen() { setTitle("UI Component Sample"); } } -
In the screen constructor, create a group of radio buttons by
using the
RadioButtonGroup class. Create the radio buttons
that you want to add to the group by using the
RadioButtonField class. In the
RadioButtonField constructor, specify the label for
the radio button, the group, and a Boolean value to indicate the default
selection (for example,true indicates that by default the
radio is selected). Invoke
add() to add the radio buttons to the screen.
RadioButtonGroup rbg = new RadioButtonGroup(); add(new RadioButtonField("Option 1",rbg,true)); add(new RadioButtonField("Option 2",rbg,false)); -
To override the default functionality that prompts the user to
save changes before the application closes, in the extension of the
MainScreen class, override the
MainScreen.onSavePrompt() method. In the following
code sample, the return value is
true which indicates that the application does not
prompt the user before closing.
public boolean onSavePrompt() { return true; }
Code sample: Creating a radio button
import net.rim.device.api.ui.*;
import net.rim.device.api.ui.component.*;
import net.rim.device.api.ui.container.*;
public class MyUi extends UiApplication
{
public static void main(String[] args)
{
MyUi theApp = new MyUi();
theApp.enterEventDispatcher();
}
public MyUi()
{
pushScreen(new MyUiScreen());
}
}
class MyUiScreen extends MainScreen
{
public MyUiScreen()
{
setTitle("UI Component Sample");
RadioButtonGroup rbg = new RadioButtonGroup();
add(new RadioButtonField("Option 1",rbg,true));
add(new RadioButtonField("Option 2",rbg,false));
}
public boolean onSavePrompt()
{
return true;
}
}
Next topic:
Activity indicators and progress indicators
Previous topic: Best practice: Implementing radio buttons