Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

UITable Callback Question

80 views
Skip to first unread message

Adam

unread,
Jun 18, 2010, 3:40:22 PM6/18/10
to
Hi,

I'm making a GUI that takes in input via a table. As the user, you would add data then click start to run the algorithm using the data from the table. My issue is that the Data in a cell is not updated until the user actually moves away from the cell. Is there a way to have the data update upon the value being changed. The table's callback also only runs once the user have moved away from the cell.

The problem is say the user changes the value in cell 1 from '3' to '5', then immediately clicks on the start button (without clicking somewhere else first). When I retrieve from the cell the value is still '3'. Is there a work around this? Perhaps, some way to force a update on the cell data.

Thanks a lot,

Adam

us

unread,
Jun 18, 2010, 4:15:18 PM6/18/10
to
"Adam " <ab...@ubc.ca> wrote in message <hvgi36$od5$1...@fred.mathworks.com>...

one of the solutions as a skeleton

uh=uitable;
set(uh,...
'data',magic(2),...
'columneditable',true,...
'celleditcallback','get(uh,''data'')');
% now change values and move the focus away from the cell...
% eg, TAB, mouse click, etc...

us

Adam

unread,
Jun 18, 2010, 4:45:23 PM6/18/10
to
"us " <u...@neurol.unizh.ch> wrote in message <hvgk4m$4qg$1...@fred.mathworks.com>...

Thanks us, that is basicly what I have now.

What I have:
set(handles.inputTable,...
'BackgroundColor',[0.93 0.93 0.93
0.7 0.9 0.9 ],...
'Units','normalized',...
'Data',dat,...
'Enable','on',...
'RowName',rownames,...
'CellEditCallback',@inputTable_Callback,...
'ColumnWidth', columnwidth,...
'ColumnName', columnname,...
'ColumnFormat', columnformat,...
'ColumnEditable', columneditable);

function inputTable_Callback(source, eventdata)
data=get(source,'Data')
numericCheck(data{eventdata.Indices(1),eventdata.Indices(2)})
end

This doesn't work since 'Data' is not changed upon keystroke, but rather when the user moves focus away from the cell.

Adam

unread,
Jun 18, 2010, 4:57:04 PM6/18/10
to
After more testing, it seems things are even worse than I thought. You must click on a cell in the table for the data to update. Even click away from the table to another textbox won't update the contents.

Walter Roberson

unread,
Jun 18, 2010, 5:40:01 PM6/18/10
to
Adam wrote:

> The problem is say the user changes the value in cell 1 from '3' to '5',
> then immediately clicks on the start button (without clicking somewhere
> else first). When I retrieve from the cell the value is still '3'. Is
> there a work around this? Perhaps, some way to force a update on the
> cell data.

It's a known limitation in all kinds of edit fields. I don't know what the
technical limitation *is*, but I gather it is considered to not be fixable.

The work-around is to use key press callbacks and to handle each key press
independently, including things like handling the pressing of backspace as
meaning you want to correct the previous character. This approach has some
difficulties as well.

My memory is a bit fuzzy at the moment (too much hot chocolate); I seem to
recall that to do the key press callbacks for a uitable that you might have to
go in at the java level. If so then the technique will be described in more
detail at http://undocumented-matlab.com

Adam

unread,
Jun 18, 2010, 5:59:06 PM6/18/10
to
Thanks, I figured there is no high-level way to do it. What I'm doing now, is using the Cell Selection Callback to find the index of the cell. Then I set the index to a handle.
As shown below:

function inputTable_CellSelectionCallback(source, eventdata)
handles=guidata(source);
handles.currentCell=eventdata.Indices
guidata(gcf,handles)
end

Then I use this index in the KeyPressFcn to manually store the data on keypress.

function inputTable_KeyPressFcn(source, eventdata)
handles=guidata(gcf);
Indices=handles.currentCell;
data=get(source,'Data');
data{Indices(1),Indices(2)}=str2double(eventdata.Key)
set(handles.inputTable,'Data',data);
end

This seems to work if only one number is inputed. However if we go to double digits, the last one gets removed. I could concatenate what's already in the cell with the new key, but as you mentioned, backspace/arrows etc. will also have to be manually programmed. I don't think other coders will like that. Addtionally, the set command causes the cell to lose focus, making cellselection give an error.

I guess I'll switch back to edit boxes or something. Thanks for the help though. Now I know it can't be done.

Yair Altman

unread,
Jun 19, 2010, 5:42:05 PM6/19/10
to
"Adam " <ab...@ubc.ca> wrote in message ...

> I guess I'll switch back to edit boxes or something. Thanks for the help though. Now I know it can't be done.

This is an intolerable snide on Java components :-)
Of course it can be done - you just need to get the relevant Java component (for example, using the FindJObj utility on the File Exchange) and then set its FocusLostCallback to trigger an editting-stop action:

mtable = uitable(...);
jscroll = findjobj(mtable);
jtable = jscroll.getViewport.getComponent(0);
hjtable = handle(jtable,'CallbackProperties');
set(hjtable, 'FocusLostCallback', @myCallbackFunc);

function myCallbackFunc(jtable,eventdata)
component = jtable.getEditorComponent;
if ~isempty(component)
event = javax.swing.event.ChangeEvent(component);
jtable.editingStopped(event);
end
end

Yair Altman
http://UndocumentedMatlab.com

Yair Altman

unread,
Jun 19, 2010, 6:05:22 PM6/19/10
to
"Yair Altman" <altma...@gmailDEL.comDEL> wrote in message <hvjdjd$69o$1...@fred.mathworks.com>...


Or an even simpler hack (see related: http://bugs.sun.com/bugdatabase/view_bug.do;:WuuT?bug_id=4709394 ):
jtable.putClientProperty('terminateEditOnFocusLost', true);

Yair

Richard Xiong

unread,
Jun 24, 2010, 1:57:05 PM6/24/10
to
jscroll = findjobj(t);
jtable = jscroll.getViewport.getComponent(0);
The value is stored in get(jtable.getEditorComponent,'text'). when hit the button I use the stored value to replace the value read from data=get(table,'Data').

Richard

"Adam " <ab...@ubc.ca> wrote in message <hvgi36$od5$1...@fred.mathworks.com>...

0 new messages