| 112 | Radio buttons present a set of choices to the user; only one choice can be selected at any time. |
| 113 | |
| 114 | There are two things to think about when using radio buttons: |
| 115 | |
| 116 | 1. putting the radio buttons into a group, so as one is selected the others become unselected. |
| 117 | 1. retrieving the selected value. |
| 118 | |
| 119 | A straightforward way to retrieve the selected value is to listen for the 'toggle' events on each radio button. When a radio button is toggled, it is selected, and a method can be called to store the relevant value. Placing radio buttons into the same group is done by passing the first radio button as an argument to the constructor for the second and subsequent radio buttons. |
| 120 | |
| 121 | {{{ |
| 122 | #!cobra |
| 123 | var _state as String # stores the current selected item in the list of radio buttons |
| 124 | |
| 125 | def selected1(obj, e) # update state as first radio button has been selected |
| 126 | _state = "Cobra" |
| 127 | |
| 128 | def createWidgets |
| 129 | # ... create first radio button |
| 130 | radio1 = RadioButton("Cobra") |
| 131 | listen radio1.toggled, ref .selected1 |
| 132 | # the second radio button uses the first, to put them in the same group |
| 133 | radio2 = RadioButton(radio1, "Java") |
| 134 | listen radio2.toggled, ref .selected2 |
| 135 | |
| 136 | }}} |
| 137 | |
| 138 | [[Image(radiobuttons.png)]] |
| 139 | |
| 143 | The combo box allows the user to select from a predefined list of items. The current selection is always shown to the user. Different variations of the combo box exist, such as allowing the user to enter some text to make a selection. Combo boxes can display different kinds of information. Before display, a 'cell renderer' must be created to control how the items in the list are displayed. And the list of items must be added to a suitable data store. |
| 144 | |
| 145 | The example here simply displays a list of strings. The renderer is an instance of '!CellRendererText', and the store is a simple '!ListStore'. The currently selected item ''index'' can be found as {{{_combobox.active}}} |
| 146 | |
| 147 | [[Image(combobox.png)]] |
| 148 | |
| 149 | {{{ |
| 150 | #!cobra |
| 151 | |
| 152 | _combobox = ComboBox() |
| 153 | _combobox.clear |
| 154 | # create and add the text renderer to the combox box |
| 155 | cell = CellRendererText() |
| 156 | _combobox.packStart(cell, false) |
| 157 | _combobox.addAttribute(cell, "text", 0) |
| 158 | # create and set the backing store of items to display |
| 159 | store = ListStore(String) |
| 160 | _combobox.model = store |
| 161 | # add string values to list store |
| 162 | for planet in ["Mercury", "Venus", "Earth", "Mars", _ |
| 163 | "Jupiter", "Saturn", "Uranus", "Neptune"] |
| 164 | store.appendValues(planet) |
| 165 | # set the index of the item to display |
| 166 | _combobox.active = 5 |
| 167 | }}} |