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

后端 未结 2 2024
离开以前
离开以前 2021-01-12 15:03

I created the following uitable:

\"bug\"

actually every single row is an indpendent

相关标签:
2条回答
  • 2021-01-12 15:40

    After some deeper research I found out, that the 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
    

    Doing this the blue highlighting is gone, BUT the dotted line around the last selected cell remains! But I found a solution resolving this, which also makes the first part dispensable.

    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
    

    Which results in the desired behaviour:

    enter image description here

    Drawback of the second solution: it lags a little (probably just on slow machines), because of the creation of a new object.

    0 讨论(0)
  • 2021-01-12 15:43

    Allright, I found a solution for deselecting cells:

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

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

    2. You need the handle of your table, lets call it mtable. Then you need the underlying Java-table and do some stuff to get the right objects and set some properties. You can do this by:

    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 tricky part (at least it was for me): If you have some Callback for CellSelectionChanged, but you dont want to excecute this now, you have to turn it off temporary:

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

    Now you can change the selection by:

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

    And now, I was expecting, when setting the CellSelectionCallback back to its original function, everything would be fine. Nope, it was excecuting the Callback. I still dont know the exact reason, but it seems to me, that calling jtable.changeSelection() the selection changes and then is calling the specified Callback, but the caller function is not waiting while this process is running. So what I tried (and I dont know if this is the best way to do it, but it is working very well) is to just pause for a second and then set the Callabck back:

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

    4. Now just one more thing: My purpose was just to change the selection to some other cell. Yours is to deselect. I dont know anything about the Java components, but I succeeded by just setting the row/column parameter to -1:

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

    Finally I managed to solve this problem by using many things explained on undocumentedmatlab.com and other posts. I am not sure if all the lines are necessary to 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 a lot of other functions/parameters etc. you can use, that are undocumented. Just to see what is possible, you can take a look with the autocomplete. Just use it on the jtable. and Tab will display them. For a documentation on those elements you should probably search for a Java-doc.

    Just a small "dynamic" minimal example (wait 3 seconds to see a 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
    
    0 讨论(0)
提交回复
热议问题