How to restore focus in uitable after CellEditCallback
3 次查看(过去 30 天)
显示 更早的评论
I have a small uitable created in GUIDE (legacy software). I want to be able to tab from cell to cell, entering numbers in columns 1 and 2 that lead to the difference being displayed in column 3 of that row. I'm using CellEditCallback to do the calculation:
function uitable1_CellEditCallback(hObject, eventdata)
row=eventdata.Indices(1);
hObject.Data(row,3)=hObject.Data(row,2)-hObject.Data(row,1);
With no code, I can tab and enter data in each cell, and at the end of the row it advances to the next cell. However, with the code above, although the calculation works and the cell selection advances to the next cell, the focus is lost, and I can't type another value. If I use an arrow key to advance to the next cell, it always returns to row 1, column 1.
As a bonus, it would also be nice if when I tab from column 2 it scrolls to the next row and skips the calculated cell in column 3.
I have tried adding drawnow() after the above. I've also tried using the scroll function by programmatically deriving the newly desired cell coordinates: scroll(hObject,'cell',newcoords) without success, getting the error "Check for missing argument or incorrect argument data type in call to function 'scroll'". Any advice most welcome.
0 个评论
回答(1 个)
Abhishek Chakram
2023-10-3
Hi Andy Smerdon.
It is my understanding that you are facing difficulty in restoring the focus back to "uitable" after "CellEditCallBack" is executed. To resolve this issue, you can use the "uicontrol" function to set the focus back to the "uitable" after the calculation is performed. Here is an example for the same:
function uitable1_CellEditCallback(hObject, eventdata)
row = eventdata.Indices(1);
hObject.Data(row, 3) = hObject.Data(row, 2) - hObject.Data(row, 1);
% Set focus back to the uitable
uicontrol(hObject);
end
By calling uicontrol(hObject), the focus will be set back to the "uitable" after the calculation is done, allowing you to continue entering values.
Regarding the scrolling to the next row and skipping the calculated cell in column 3 when tabbing from column 2, you can achieve this by implementing a custom "KeyPressFcn" for the "uitable". Here is an example for the same:
function uitable1_KeyPressFcn(hObject, eventdata)
if strcmp(eventdata.Key, 'tab')
% Get current cell position
currentPosition = hObject.CurrentCell;
currentRow = currentPosition(1);
currentCol = currentPosition(2);
% Calculate next cell position
nextRow = currentRow + 1;
nextCol = currentCol;
% Skip column 3 if tabbing from column 2
if currentCol == 2
nextCol = currentCol + 1;
end
% Scroll to the next cell
hObject.CurrentCell = [nextRow, nextCol];
end
end
In this example, the "KeyPressFcn" of the "uitable" is used to detect the tab key press. If the tab key is pressed, it calculates the position of the next cell based on the current cell position. If tab key is pressed from column 2, it skips column 3 and moves to the next row. Finally, it scrolls to the next cell by setting "hObject.CurrentCell" to the desired position.
Make sure to assign the "KeyPressFcn" callback to your "uitable" in the GUIDE interface by selecting the "uitable", going to the Property Inspector, and setting the "KeyPressFcn" property to the name of your callback function
You can refer to the following documentation to know more about the functions used:
Best Regards,
Abhishek Chakram
2 个评论
Voss
2023-10-26
@Abhishek Chakram: Is CurrentCell a documented property of uitables? I can't find it in the list:
Is it similar to the Selection property?
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Migrate GUIDE Apps 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!