The Accidental Rubyist

invalid byte sequence in UTF-8

Editable lists: Fields, Checkboxes, ComboBoxes

leave a comment »

Thanks to CellRenderers and CellEditors, we may edit existing lists with just this line added:

  cell_editing_allowed true

If you want row selectors to be shown:

    show_selector true

The selector itself may be changed, the default is a gt sign: “>”

Editable list using Field class

Editable list using Field class

Since the rows are selectable, therefore you can make a list of items, and select some as you would in a checklist, or todo list. However, now the user can edit and add to the list also. However, editing facilities means that keys for selection and navigation need to be selected so they do not clash with editing keys. Earlier, space bar selected.

Here is the result on a list of strings. When the cursor falls on the row, the default CellEditor which uses a Field is used for accepting keys. You may extend and install your own editor, as we have done for checkboxes and combos.

By default, boolean values would show as “true” and “false”, so we needed to create a CheckBoxCellRenderer, that display true and false just as a checkbox does. As far as the CellEditor is concerned, just pass a Checkbox in the constructor. It will be reused for editing each row.(link)

The 2 important lines are:

          # create and set a cell renderer

          cell_renderer nil, {"parent" => self, "display_length"=> @width-2}

          # create and set a cell editor, passing a checkbox
          cell_editor nil, {"focusable"=>false, "visible"=>false})


A listing of the box code is as follows. alist is the list of boolean values.

        alist = [true, false, true, false, true, false, true, false, true]
        cblist = alist
        listcb = @form do
          name   "cblist"
          row  1
          col  96
          width 8
          height 10
#         list mylist
          list_variable cblist
          #selection_mode :SINGLE
          title "Checklist"
          title_attrib 'reverse'
          cell_renderer nil, {"parent" => self, "display_length"=> @width-2}
          cell_editing_allowed true
          cell_editor nil, {"focusable"=>false, "visible"=>false})

The above will be of more practical value when we allow editable multi-column tables, with columns containing Fields, checkboxes etc.

A list of editable checkboxes, and one of combolists

A list of editable checkboxes, and one of combolists

The next example is of a list of comboboxes. The main lines are those setting the CellRenderer (not really required, i think, since the combobox extends Field. However, the editor is created with a combobox passed in the constructor of the editor. Note that the constructor of the combobox must pass in the list or list_data_model, as before.

          cell_renderer nil, {"parent" => self, "display_length"=> width()-2}
          cell_editing_allowed true
          cell_editor nil, {"focusable"=>false, "visible"=>false, "list"=>colist, "display_length"=>width()-2})


Written by totalrecall

January 11, 2009 at 9:41 pm

Posted in rbcurse, ruby

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: