Package totalcross.ui

Class Radio

  • All Implemented Interfaces:
    MaterialEffect.SideEffect, TextControl

    public class Radio
    extends Control
    implements TextControl, MaterialEffect.SideEffect
    Radio is a radio control. Radios can be grouped together using a RadioGroupController.

    Here is an example showing a radio being used:

     public class MyProgram extends MainWindow
     {
        RadioGroupController rgGender;
    
        public void initUI()
        {
           rgGender = new RadioGroupController();
           add(new Radio("Male", rgGender), LEFT, AFTER);
           add(new Radio("Female", rgGender), AFTER+2, SAME);
           rgGender.setSelectedIndex(radioMale); // activate the specified one.
        }
    
        public void onEvent(Event event)
        {
           if (event.type == ControlEvent.PRESSED && (event.target instanceof Radio) && ((Radio)event.target).getRadioGroup() == rgGender)
           {
              boolean male = rgGender.getSelectedIndex() == 0;
              ... handle radio Male being pressed
     
    See Also:
    RadioGroupController
    • Field Detail

      • autoSplit

        public boolean autoSplit
      • textColor

        public int textColor
        Sets the text color of the check. Defaults to the foreground color.
        Since:
        TotalCross 2.0.
      • checkColor

        public int checkColor
        Set to the color of the check, if you want to make it different of the foreground color.
        Since:
        TotalCross 1.3
      • leftJustify

        public boolean leftJustify
        Set to true to left justify this control if the width is above the preferred one.

        Attention: You can change the gap between the radio and the text by changing the variable "radioTextGap" by calling setRadioTextGap(int gap)

    • Constructor Detail

      • Radio

        public Radio​(java.lang.String text)
        Creates a radio control displaying the given text.
      • Radio

        public Radio​(java.lang.String text,
                     RadioGroupController radioGroup)
        Creates a radio control with the given text attached to the given RadioGroupController
    • Method Detail

      • getRadioGroup

        public RadioGroupController getRadioGroup()
        Returns the RadioGroupController that this radio belongs to, or null if none.
      • setText

        public void setText​(java.lang.String text)
        Sets the text.
        Specified by:
        setText in interface TextControl
      • getText

        public java.lang.String getText()
        Gets the text displayed in the radio.
        Specified by:
        getText in interface TextControl
      • isChecked

        public boolean isChecked()
        Returns the checked state of the control.
      • setChecked

        public void setChecked​(boolean checked)
        Sets the checked state of the control.
      • setChecked

        public void setChecked​(boolean checked,
                               boolean sendPress)
        Sets the checked state of the control, and send the press event if desired.
      • getPreferredWidth

        public int getPreferredWidth()
        returns the preferred width of this control.
        Overrides:
        getPreferredWidth in class Control
      • getPreferredHeight

        public int getPreferredHeight()
        returns the preferred height of this control.
        Overrides:
        getPreferredHeight in class Control
      • onEvent

        public void onEvent​(Event event)
        Called by the system to pass events to the radio control.
        Overrides:
        onEvent in class Control
        Parameters:
        event - the event to process
        See Also:
        Event, KeyEvent, PenEvent
      • onColorsChanged

        protected void onColorsChanged​(boolean colorsChanged)
        Description copied from class: Control
        Called after a setEnabled, setForeColor and setBackColor and when a control has been added to a Container. If colorsChanged is true, it was called from setForeColor/setBackColor/Container.add; otherwise, it was called from setEnabled
        Overrides:
        onColorsChanged in class Control
      • onFontChanged

        protected void onFontChanged()
        Description copied from class: Control
        Called after a setFont
        Overrides:
        onFontChanged in class Control
      • onPaint

        public void onPaint​(Graphics g)
        Called by the system to draw the radio control.
        Overrides:
        onPaint in class Control
        Parameters:
        g - the graphics object for drawing
        See Also:
        Graphics
      • clear

        public void clear()
        Clears this control, checking it if clearValueInt is 1.
        Overrides:
        clear in class Control
      • onBoundsChanged

        protected void onBoundsChanged​(boolean screenChanged)
        Description copied from class: Control
        Called after a setRect.
        Overrides:
        onBoundsChanged in class Control
        Parameters:
        screenChanged - If the bounds were changed due to a screen change (rotation, collapse)
      • split

        public void split​(int maxWidth)
        Splits the text to the given width.
        Since:
        TotalCross 4.2.0
        See Also:
        autoSplit
      • setRadioTextGap

        public void setRadioTextGap​(int gap)
        The gap between the radio and the text when the leftJustify is set to true.
      • getRadioTextGap

        public int getRadioTextGap()
        The gap between the radio and the text.