Namespaces

Variants
Actions

Please note that as of October 24, 2014, the Nokia Developer Wiki will no longer be accepting user contributions, including new entries, edits and comments, as we begin transitioning to our new home, in the Windows Phone Development Wiki. We plan to move over the majority of the existing entries over the next few weeks. Thanks for all your past and future contributions.

Using radio buttons in Java ME

From Wiki
Jump to: navigation, search

This code snippet shows how to create and manipulate the radio buttons control.

Article Metadata
Code ExampleTested with
Devices(s): Nokia N78, Nokia N79, Nokia N85, Nokia 5800 XpressMusic, Nokia 6131, Nokia 2630
Compatibility
Platform(s): S60 3rd Edition FP2, S60 5th Edition, Series 40 3rd Edition FP1, Series 40 5th Edition LE
Series 40 3rd Edition FP1
S60 5th Edition
S60 3rd Edition FP2
Article
Created: IlGolub (25 Feb 2009)
Last edited: hamishwillee (08 Aug 2013)

Contents

Overview

A radio button object is created based on the ChoiceGroup class of EXCLUSIVE type. For adding a radio button item, the method append() of the ChoiceGroup object is used. For deleting the selected radio button, the method delete() of the ChoiceGroup object is used, by implementing the ItemCommandListener interface of the MIDlet class.

Source file: RadiobuttonsMIDlet.java

    /**
* ChoiceGroup object is the visual component which can be represented as
* radiobuttons object within Form object
*/

private ChoiceGroup radiobuttonsObj;
/**
* incremental id's counter for radiobuttons objects
*/

private int radiobuttonsIdCounter;
 
/**
* Executes the snippet.
* Implementation appends Radiobuttons object to mainForm object,
* autocreates some radiobuttons items and
* add two additional commands(one to Form and other to ChoiceGroup)
*/

private void executeSnippet() {
String text = "Snippet executed...";
printString(text);
//adds RadiobuttonsObj object to our form
mainForm.insert( 0 ,getRadiobuttonsObj());
 
//adds delete radiobutton item command to RadiobuttonsObj object
getRadiobuttonsObj().addCommand(DELETE_RADIOBUTTON_COMMAND);
//sets up commands handler for RadiobuttonsObj object
getRadiobuttonsObj().setItemCommandListener((ItemCommandListener) this);
//adds add radiobutton item command to RadiobuttonsObj object
mainForm.addCommand(ADD_RADIOBUTTON_COMMAND);
printString("Radiobuttons object successfully created!");
 
try {
logPrintString(text);
} catch (IOException ex) {
printString(ex.getMessage());
}
}
/**
* From CommandListener.
* Called by the system to indicate that a command has been invoked on a
* particular displayable.
* @param command the command that was invoked
* @param displayable the displayable where the command was invoked
*/

public void commandAction(Command command, Displayable displayable) {
if (command == EXIT_COMMAND) {
// Exit the MIDlet
exit();
} else if (command == EXECUTE_COMMAND) {
// Execute the snippet
executeSnippet();
//remove execute command for safe working(we don't want second
//attemp to create radiobuttons object)
mainForm.removeCommand(EXECUTE_COMMAND);
} else if (command == ADD_RADIOBUTTON_COMMAND) {
printString("starting radiobutton item creation");
String newrbuttonString = "Radio " + radiobuttonsIdCounter;
radiobuttonsIdCounter++;
//adding new radiobutton item
getRadiobuttonsObj().append(newrbuttonString, null);
printString("new radiobutton item successfully created!");
}
}
/**
* Method creates ChoiceGroup object of "radiobuttons" type if it is not
* exist and return the reference to it, or return the reference
* to existing object.
* @return the reference to ChoiceGroup object
*/

private ChoiceGroup getRadiobuttonsObj() {
if(radiobuttonsObj == null) {
String[] rButtonsStrings = new String[3];
 
//autocreating of names array for three radiobutton items
for( radiobuttonsIdCounter = 0;
radiobuttonsIdCounter<3;
radiobuttonsIdCounter++) {
rButtonsStrings[radiobuttonsIdCounter] =
"radio " + radiobuttonsIdCounter;
}
//creating the ChoiceGroup object of radiobuttons type
radiobuttonsObj = new ChoiceGroup("Radio Buttons!",
ChoiceGroup.EXCLUSIVE,
rButtonsStrings, null);
}
return radiobuttonsObj;
}
/**
* Implements the ItemCommandListener interface for our form.
* It provides commands handling from Item's within Form object.
* @param command - some type of Command object
* @param item - Item object which owns command object
*/

public void commandAction(Command command, Item item) {
if (command == DELETE_RADIOBUTTON_COMMAND &&
item == getRadiobuttonsObj()) {
printString("starting selected radiobutton item deleting");
//getting index of selected item
int selectedIndex = getRadiobuttonsObj().getSelectedIndex();
//deleting selected radiobutton item
getRadiobuttonsObj().delete(selectedIndex);
printString("new radiobutton item successfully created!");
}
}

Postconditions

When the snippet is executed, the MIDlet creates a radiobuttonsObj object and adds two commands - 'Add new rbutton' for the Form object and 'Delete new rbutton' for the radiobuttonsObj object. Use these commands to add or delete radio buttons.

Supplementary material

This code snippet is part of the stub concept, which means that it has been patched on top of a template application in order to be more useful for developers. The version of the Java ME stub application used as a template in this snippet is v1.1.

This page was last modified on 8 August 2013, at 03:49.
80 page views in the last 30 days.

Was this page helpful?

Your feedback about this content is important. Let us know what you think.

 

Thank you!

We appreciate your feedback.

×