How to unselect cells in uitable / how to disable cell selection highlighting?

I created the following uitable :

bug

in fact, each individual line is independent uitable , so the image shown contains 5 elements in addition to the title. Why I am doing this is the question of my last question , which results in a table showing. You can find the fully executable code in the answer here (or the minimal example below). Solutions using a common graphical interface also exist, but that would have exploded the code too much, and in fact it just looks like an error.

You can see that every time I move to the next line, therefore, to another, useful, the last choice remains highlighted, which looks silly, although it does not matter for functionality.

There is a 'SelectionHighlight' property for uitables, it sounds like a solution, but it does not change anything. I used it like:

 set(src,'SelectionHighlight','off') %where src is the handle of the current uitable 

in different places: at the end of a 'CellSelectionCallback' , at the end of a 'CellEditCallback' and as a global property. But every time the last cell remains selected. Actually, I don’t need a choice at all.

How do I turn off an entire selection or selection element for all my elements?

How do I use this property to have an effect?

Alternatively: how to change the highlight color (and therefore the text color) so that the highlight is no longer displayed?

Apparently, this problem also appears in other contexts .


I created a minimal executable example in which you can select a number from 1 to 3 in each row.

 function minimalTable %basic properties line_height = 21.32; table_height = 3*line_height; lh = line_height/table_height; h = figure('Position',[200 100 202 table_height],'numbertitle','off','MenuBar','none'); % addrow(figurehandle,number of row, percentage lineheight) % every function call creates a new row, later dynamically addRow(h,1,lh); addRow(h,2,lh); addRow(h,3,lh); end function modifySelection(src,~) set(src,'SelectionHighlight','off') waitfor(src) end function [th] = addRow(fh,k,lhp) selector = { '1'; '2' ; '3' }; defaultData = {'select number...'}; columnformat = { {selector{:}} }; columneditable = true; th = uitable(fh,'Units','normalized','Position',[0 1-k*lhp 1 lhp],... 'Data', defaultData,... 'ColumnName', [],... 'ColumnWidth', {200},... 'ColumnEditable', columneditable,... 'ColumnFormat', columnformat,... 'RowName',[],... 'SelectionHighlight','off',... 'CellEditCallback',@modifySelection); end 

leads to:

min_example

+6
source share
2 answers

After some more in-depth research, it turned out that Matlab support comes out with the following solution:

 %overwrite data with a dummy and restore the old data afterwards, to force deselection function modifySelection(src,~) ... temp = get(src,'Data') set(src,'Data',{ 'dummy' }); set(src,'Data', temp ); end 

The execution of this blue selection has disappeared, BUT the dashed line around the last selected cell remains! But I found a solution that resolves this, which also makes the first part available.

 function modifySelection(src,evt) ... fh = get(src,'parent'); % get parent figure handle copyobj(src,fh); % copy uitable to parent figure delete(src); % delete current uitable end 

This leads to the desired behavior:

enter image description here

The disadvantage of the second solution: it is slightly behind (perhaps only from slow machines), due to the creation of a new object.

+4
source

Ok, I found a solution to deselect the cells:

First of all, this requires some Java. But don’t worry, it will still look like Matlab :)

1. You will need the script findjobj from Yair Altman: TMW File-Exchange: findjobj

2. You need a handle to your table, name it mtable. Then you need a basic Java table and do some things to get the objects you need and set some properties. You can do it:

 jscroll=findjobj(mtable); h=jscroll.getComponents; viewport=h(1); a=viewport.getComponents; jtable=a(1); %com.mathworks.hg.peer.ui.UITablePeer jtable.setRowSelectionAllowed(0); jtable.setColumnSelectionAllowed(0); 

3. Now the more difficult part (at least it was for me): if you have a Callback for CellSelectionChanged, but you do not want to cancel it, you should disable it temporarily

 set(mtable, 'CellSelectionCallback', []); 

Now you can change the selection:

 jtable.changeSelection(row-1,col-1, false, false); %Java-> zero ^= one <-Matlab 

And now, I was expecting when the CellSelectionCallback setting returned to its original function, everything would be fine. No, that was a Callback exception. I still don't know the exact reason, but it seems to me that calling jtable.changeSelection () changes the selection and then calls the specified callback, but the caller function does not wait until this process is started. So what I tried (and I don’t know if this is the best way to do this, but it works very well) is to just pause for a second and then return Callabck back:

 pause(1) set(mtable, 'CellSelectionCallback', @myOriginalFunction); 

4. Now one more thing: my goal was to change the selection to another cell. You must deselect. I don't know anything about Java components, but I was able to just set the row / column parameter to -1:

 jtable.changeSelection(-1,-1, false, false); 

Finally, I was able to solve this problem using a lot of explainable on undocumentedmatlab.com and other posts. I'm not sure if all the lines are needed for the call. Note that this will only be available for the documented Matlab-uitable, which appears first in version 2008 (a or b, I'm not sure about that).

EDIT There are many other functions / parameters, etc. that you can use that are undocumented. To see what is possible, you can take a look at autocomplete. Just use it on jtable. and Tab will display them. For documentation on these elements, you should probably look for a Java document.

Just a small “dynamic” minimal example (wait 3 seconds to see the change ;-)):

 function startUitable() xDat=ones(5,3); h=figure('Tag','TestFigure'); mtable=uitable('Tag','TestUITABLE'); rowField=uicontrol('units','normalized','Style','edit','Position',[0.4 0.9 0.1 0.1],'parent',h,'Tag','rowField'); colField=uicontrol('units','normalized','Style','edit','Position',[0.6 0.9 0.1 0.1],'parent',h,'Tag','colField'); set(mtable, 'Units','normalized','Position',... [0.01 0.01 0.8 0.8], 'Data', xDat,... 'ColumnEditable', [false, false,false],... 'ColumnWidth', 'auto') myButton=uicontrol('units','normalized','Style','pushbutton','parent',h,'Position',[0.04 0.9 0.3 0.1],'String','change Selection') set(myButton,'Callback',@changeSelection) end function changeSelection(~,~,~) mtable=findobj('Tag','TestUITABLE'); jscroll=findjobj(mtable); h=jscroll.getComponents; viewport=h(1); a=viewport.getComponents; jtable=a(1); %com.mathworks.hg.peer.ui.UITablePeer % jtable.setRowSelectionAllowed(0); % jtable.setColumnSelectionAllowed(0); row=str2num(get(findobj('Tag','rowField'),'String')); col=str2num(get(findobj('Tag','colField'),'String')); jtable.changeSelection(row-1,col-1, false, false); end 
+1
source

Source: https://habr.com/ru/post/956868/


All Articles