Thursday February 09, 2006 There was a question on nbusers mailing list if it is possible to edit the individual cells in the TableItem component provided by the Mobility Pack 5.0. Even though the TableItem component does not contain functionality which would let users directly edit the individual cells in the table, it is still possible to let the users edit the cells using the following technique:.


if (item == tableItem1) {
if (command == itemCommand1) {
// Insert pre-action code here
getDisplay().setCurrent(get_tableItemEditBox());
// Insert post-action code here
}
}
}
Now you need to add the code before the display is going to switch to the text box, which will get a value of currently selected cell from the table item and assign it to the text box, in which the user is going to edit the value. This can be done quite easily - you need to obtain column and row of the currently selected table item cell and set this value to the text box. The code might look like the following:
if (item == tableItem1) {
if (command == itemCommand1) {
// insert value from the currently selected cell in the table item to the text box
final int selectedRow = get_tableItem1().getSelectedCellRow(); // get selected row
final int selectedColumn = get_tableItem1().getSelectedCellColumn(); // get selected column
final String cellValue = (String)get_simpleTableModel1().getValue(selectedColumn,selectedRow); // get value from the model
get_tableItemEditBox().setString(cellValue); // set the value to the text box
getDisplay().setCurrent(get_tableItemEditBox());
// Insert post-action code here
}
}
}
Now you should be able to edit the cell value, but the edited value should get somehow back to the table. To do so, you need to add some more code to the Ok action on the text box component. Go to the flow designer, right-click on the line going from ok command from the tableItemEditBox and select Go To Source from the pop-up menu. This opens the source and the cursor is positioned at the code which switches from the text box back to the form with the table item. Here you can type the code which will get the edited value from the tableItemEditBox component, update the table model with it and then fire event that the table model has changed (so the table item can relayout the table grid based on the new values). The code could look like the following:
} else if (displayable == tableItemEditBox) {
if (command == okCommand1) {
// need to update the value of the edited cell
final String value = get_tableItemEditBox().getString();
final int selectedRow = get_tableItem1().getSelectedCellRow();
final int selectedColumn = get_tableItem1().getSelectedCellColumn();
get_simpleTableModel1().setValue(selectedColumn,selectedRow,value);
// don't forget to fire table model changed event, so the table item gets relayouted
get_simpleTableModel1().fireTableModelChanged();
getDisplay().setCurrent(get_form1());
// Insert post-action code here
} else if (command == cancelCommand1) {
// just return from the textbox - no change in the tableModel for cancel action
getDisplay().setCurrent(get_form1());
}
}
And that's it - now you should have the functionality for editing the cells of the table item. You can download an archive of the project which contains all this functionality and you can use it as you wish. I hope this mini how-to helps a bit when working with the new custom components available in the Mobility Pack 5.0.
Posted by Lukas on February 09, 2006 at 03:52 PM CET #
Posted by Breh on February 09, 2006 at 04:02 PM CET #
Posted by Lukas on February 09, 2006 at 04:03 PM CET #