Discover MakerZone

MATLAB and Simulink resources for Arduino, LEGO, and Raspberry Pi

Learn more

Discover what MATLAB® can do for your career.

Opportunities for recent engineering grads.

Apply Today

Thread Subject:
UITable Callback Question

Subject: UITable Callback Question

From: Adam

Date: 18 Jun, 2010 19:40:22

Message: 1 of 9

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

Subject: UITable Callback Question

From: us

Date: 18 Jun, 2010 20:15:18

Message: 2 of 9

"Adam " <abc5@ubc.ca> wrote in message <hvgi36$od5$1@fred.mathworks.com>...
> 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

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

Subject: UITable Callback Question

From: Adam

Date: 18 Jun, 2010 20:45:23

Message: 3 of 9

"us " <us@neurol.unizh.ch> wrote in message <hvgk4m$4qg$1@fred.mathworks.com>...
> "Adam " <abc5@ubc.ca> wrote in message <hvgi36$od5$1@fred.mathworks.com>...
> > 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
>
> 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

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.

Subject: UITable Callback Question

From: Adam

Date: 18 Jun, 2010 20:57:04

Message: 4 of 9

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.

Subject: UITable Callback Question

From: Walter Roberson

Date: 18 Jun, 2010 21:40:01

Message: 5 of 9

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

Subject: UITable Callback Question

From: Adam

Date: 18 Jun, 2010 21:59:06

Message: 6 of 9

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.

Subject: UITable Callback Question

From: Yair Altman

Date: 19 Jun, 2010 21:42:05

Message: 7 of 9

"Adam " <abc5@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

Subject: UITable Callback Question

From: Yair Altman

Date: 19 Jun, 2010 22:05:22

Message: 8 of 9

"Yair Altman" <altmanyDEL@gmailDEL.comDEL> wrote in message <hvjdjd$69o$1@fred.mathworks.com>...
> "Adam " <abc5@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


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

Yair

Subject: UITable Callback Question

From: Richard Xiong

Date: 24 Jun, 2010 17:57:05

Message: 9 of 9

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 " <abc5@ubc.ca> wrote in message <hvgi36$od5$1@fred.mathworks.com>...
> 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

Tags for this Thread

What are tags?

A tag is like a keyword or category label associated with each thread. Tags make it easier for you to find threads of interest.

Anyone can tag a thread. Tags are public and visible to everyone.

Contact us