org.j4me.ui.components
Class RadioButton

java.lang.Object
  extended by org.j4me.ui.components.Component
      extended by org.j4me.ui.components.RadioButton

public class RadioButton
extends Component

A RadioButton component lets a user select exactly one choice from a list. An example of where radio buttons are useful is to select a logging level. There are a known set of choices and the level is set to one of them.

Traditionally radio buttons are displayed next to each other in a box. This component forgoes that model to save screen space. Instead the selected option is displayed. If the user pressed a button while the component is highlighted it takes them to a screen, similar to a menu, where the user can choose a different value.

See Also:
ChoiceGroup

Field Summary
protected  java.util.Vector choices
          The ordered list of radio button choices.
protected  int selected
          The index of the currently selected choice.
 
Fields inherited from class org.j4me.ui.components.Component
container, HIGHLIGHTED_BORDER_WIDTH
 
Constructor Summary
RadioButton()
          Constructs a radio button component.
 
Method Summary
 boolean acceptsInput()
          Tells if this component accepts user input or not.
 int append(java.lang.String choice)
          Appends an element to the list of choices.
 void delete(int elementNum)
          Deletes the element referenced by elementNum.
 void deleteAll()
          Deletes all elements from the list of choices.
 java.lang.String getLabel()
           
protected  int[] getPreferredComponentSize(Theme theme, int viewportWidth, int viewportHeight)
          Returns the ideal size for a radio button component.
 int getSelectedIndex()
          Returns the index number of the element that is selected.
 java.lang.String getSelectedValue()
          Returns the value of the element that is selected.
 java.lang.String getString(int elementNum)
          Gets the String for the element referenced by elementNum.
protected  void hideNotify()
          An event raised whenever the component is removed from the screen.
 void insert(int elementNum, java.lang.String choice)
          Inserts an element into the list of choices just prior to the element specified.
 void keyPressed(int keyCode)
          Called when a key is pressed.
protected  void paintComponent(javax.microedition.lcdui.Graphics g, Theme theme, int width, int height, boolean selected)
          Paints the radio button component.
 void pointerPressed(int x, int y)
          Called when the pointer is pressed.
protected  void select()
          Called when the radio button's value is being edited.
 void set(int elementNum, java.lang.String choice)
          Sets the element referenced by elementNum, replacing the previous contents of the element.
 void setLabel(java.lang.String label)
           
 void setSelectedIndex(int elementNum)
          Sets the index of the selected element.
protected  void showNotify()
          An event raised whenever the component is made visible on the screen.
 int size()
          Returns the number of elements to choose from.
 
Methods inherited from class org.j4me.ui.components.Component
getHeight, getHorizontalAlignment, getPreferredSize, getScreen, getWidth, getX, getY, invalidate, isShown, keyReleased, keyRepeated, paint, paintRect, pointerDragged, pointerReleased, repaint, setHorizontalAlignment, visible
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

choices

protected java.util.Vector choices
The ordered list of radio button choices.


selected

protected int selected
The index of the currently selected choice.

Constructor Detail

RadioButton

public RadioButton()
Constructs a radio button component.

Method Detail

getLabel

public java.lang.String getLabel()
Returns:
The text that appears above the radio button. If null there is no text.

setLabel

public void setLabel(java.lang.String label)
Parameters:
label - is the text that appears above the radio button. If null there will be no text.

size

public int size()
Returns the number of elements to choose from.

Returns:
The number of elements to choose from.

getString

public java.lang.String getString(int elementNum)
Gets the String for the element referenced by elementNum.

Parameters:
elementNum - is the index of the element to be queried.
Returns:
The string for the element.

append

public int append(java.lang.String choice)
Appends an element to the list of choices.

Parameters:
choice - is the string to be added.
Returns:
The assigned index of the element.

insert

public void insert(int elementNum,
                   java.lang.String choice)
Inserts an element into the list of choices just prior to the element specified.

Parameters:
elementNum - is the index of the element where insertion is to occur.
choice - is the string to be inserted.

delete

public void delete(int elementNum)
Deletes the element referenced by elementNum.

Parameters:
elementNum - is the index of the element to be deleted.

deleteAll

public void deleteAll()
Deletes all elements from the list of choices.


set

public void set(int elementNum,
                java.lang.String choice)
Sets the element referenced by elementNum, replacing the previous contents of the element.

Parameters:
elementNum - the index of the element to be set.
choice - is the string to be set.

getSelectedIndex

public int getSelectedIndex()
Returns the index number of the element that is selected.

Returns:
The index of selected element.

getSelectedValue

public java.lang.String getSelectedValue()
Returns the value of the element that is selected.

Returns:
The value of the selected element.

setSelectedIndex

public void setSelectedIndex(int elementNum)
Sets the index of the selected element.

Parameters:
elementNum - is the currently selected element.

showNotify

protected void showNotify()
An event raised whenever the component is made visible on the screen. This is called before the paintComponent method.

Overrides:
showNotify in class Component
See Also:
Component.showNotify()

hideNotify

protected void hideNotify()
An event raised whenever the component is removed from the screen.

Overrides:
hideNotify in class Component
See Also:
Component.hideNotify()

paintComponent

protected void paintComponent(javax.microedition.lcdui.Graphics g,
                              Theme theme,
                              int width,
                              int height,
                              boolean selected)
Paints the radio button component. It is shown as a text box with the currently selected value in it.

Changing the selected value is done on a different screen. Therefore it is not painted here.

Specified by:
paintComponent in class Component
Parameters:
g - is the Graphics object to be used for rendering the item.
theme - is the application's theme. Use it to get fonts and colors.
width - is the width, in pixels, to paint the component.
height - is the height, in pixels, to paint the component.
selected - is true when this components is currently selected and false when it is not.
See Also:
Component.paintComponent(javax.microedition.lcdui.Graphics, org.j4me.ui.Theme, int, int, boolean)

getPreferredComponentSize

protected int[] getPreferredComponentSize(Theme theme,
                                          int viewportWidth,
                                          int viewportHeight)
Returns the ideal size for a radio button component.

Specified by:
getPreferredComponentSize in class Component
Parameters:
theme - is the application's Theme.
viewportWidth - is the width of the viewable area, in pixels, the component can use.
viewportHeight - is the height of the viewable area, in pixels, the component can use.
Returns:
A array with two elements where the first is the width of the component in pixels and the second is the height.
See Also:
Component.getPreferredComponentSize(org.j4me.ui.Theme, int, int)

acceptsInput

public boolean acceptsInput()
Description copied from class: Component
Tells if this component accepts user input or not. If it does then it can be scrolled to by the user. If it does not, it will be displayed, but can be skipped over by scrolling.

The default implementation returns false. Override this method to return true if the component accepts input.

Overrides:
acceptsInput in class Component
Returns:
true because this component accepts user input.

keyPressed

public void keyPressed(int keyCode)
Called when a key is pressed.

Overrides:
keyPressed in class Component
Parameters:
keyCode - is the key code of the key that was pressed.

pointerPressed

public void pointerPressed(int x,
                           int y)
Called when the pointer is pressed.

Overrides:
pointerPressed in class Component
Parameters:
x - is the horizontal location where the pointer was pressed relative to the top-left corner of the component.
y - is the vertical location where the pointer was pressed relative to the top-left corner of the component.

select

protected void select()
Called when the radio button's value is being edited. This method replaces the current screen with a menu of all the available options. When the user selects a new option the original screen returns and is updated with the new value.