2

The table cell is edit with a simple click, I want it to be edit only on double click. Simple click will select the cell.

I'm use this property of uitable:

set(hTable, 'Data',data,...
    'ColumnEditable', edit,...
Robert Seifert
  • 25,078
  • 11
  • 68
  • 113
TimeIsNear
  • 711
  • 4
  • 19
  • 38

2 Answers2

5

First you need to set the cell editabiliy to false by default:

set(hTable,'ColumnEditable', [false false ...]);   %accordingly your number of columns

and introduce a CellSelectionCallback:

set(hTable,'CellSelectionCallback',@cellSelect);

which calls the following function within the same script

function cellSelect(src,evt)
getstate = get(src,'ColumnEditable');  %gets status of editability
index = evt.Indices;                   %index of clicked cell
state = [false false ...];             %set all cells to default: not editable
state(index) = ~getstate(index);       %except the clicked one, was it 
                                       %already false before set it true
set(src,'ColumnEditable', state)       %pass property to table
end

and also a CellEditCallback:

set(hTable,'CellEditCallback',@cellEdit);

calling

function cellEdit(src,~)
state = [false false ...];
set(src,'ColumnEditable', state)
end

minimal example

function minimalTable 

h = figure('Position',[600 400 402 100],'numbertitle','off','MenuBar','none');
defaultData  =  {'insert number...' , 'insert number...'};
uitable(h,'Units','normalized','Position',[0 0 1 1],...
              'Data', defaultData,... 
              'ColumnName', [],'RowName',[],...
              'ColumnWidth', {200 200},...
              'ColumnEditable', [false false],...
              'ColumnFormat', {'numeric' , 'numeric'},...  
              'CellSelectionCallback',@cellSelect);

end

function cellSelect(src,evt)
getstate = get(src,'ColumnEditable');
index = evt.Indices;
state = [false false];
state(index) = ~getstate(index);
set(src,'ColumnEditable', state)
end

function cellEdit(src,~)
state = [false false];
set(src,'ColumnEditable', state)
end

As you figured out this is not always working. Because you have the same issues like I had before with popup menus. It's exactly the same problem: ColumnEditable is just a row vector and not a matrix. I had to deal with the ColumnFormat property, which is also just a row vector. If the double click feature is really important to you, you can consult the following two answers:

Is it possible to prevent an uitable popup menu from popping up? Or: How to get a callback by clicking a cell, returning the row & column index?

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

The threads basically suggest to create a unique uitable for every single row, so that every single row has a unique ColumnEditable property. That's the only solution so far.

I'm afraid there is no simple solution. I can't offer you further help, except the complicated workarounds of the other answers. Or just use the simple one above and live with the little drawbacks.

Community
  • 1
  • 1
Robert Seifert
  • 25,078
  • 11
  • 68
  • 113
  • Iget an error: Error in ==> cellSelect at 5 state(index) = ~getstate(index); I've 4 columns In my table should I write "state = [false false false false];" ? – TimeIsNear Nov 05 '13 at 14:13
  • yes, you have to write it accordingly to your number of columns. – Robert Seifert Nov 05 '13 at 14:14
  • Sometime it work fine sometime no, cell edit on simple click :( – TimeIsNear Nov 05 '13 at 14:31
  • can you post your current code please, so I can have look at it? And is the minimal example working for you? – Robert Seifert Nov 05 '13 at 14:33
  • the only situation it would work, would be right after "selecting". But you actually would click another cell then. – Robert Seifert Nov 05 '13 at 14:35
  • It work fine with one row, but more it don't work, try with: defaultData = [{'insert number...' , 'insert number...'};{'insert number...' , 'insert number...'}]; – TimeIsNear Nov 05 '13 at 14:38
  • yes I understand the issue, I missed it. Read my final answer and the links there. I'm afraid I can't help you more on that. – Robert Seifert Nov 05 '13 at 14:54
  • Thanks so much for your help! The objective was to delete the selected rows of a table by clicking on the delete button. So I think to add a column with checkbox. I encounter other problems, I created a new issue for it. – TimeIsNear Nov 05 '13 at 15:02
  • That's the objective? I would have an elegant solution on that, without a checkbox. Do you want to ask exactly that in another question? Then I post it there. – Robert Seifert Nov 05 '13 at 15:33
  • I am very interested in your solution, I created the question could you look? thank you. The question is: "Add a checkbox column to table with default value to false" – TimeIsNear Nov 05 '13 at 15:56
  • but that's the checkbox question. Edit it to something like "How to delete a selected row in uitable?" – Robert Seifert Nov 05 '13 at 15:57
2

Although this thread is old but in my opinion still valuable to some users. I have tested the following with R2010b 32bit.

I have achieved editing only on double click simply by setting

set(hTable,'CellSelectionCallback',@tableCellSelectCB,'ColumnEditable',true)

and defining its function the following

function tableCellSelectCB(~,~)
    try
            h.jtable.getCellEditor().stopCellEditing();
    catch
    end
end

where h.jtable refers to the underlying java object of your uitable.

This way, I can select even single and multiple cells, without going into edit mode. On a double click on a single cells lets me edit its contents.

Extension to have individual editable rows

I wanted to have checkboxes in the top row and non-editable (not directly at least) data in the rest of the table. You can easily modify the above:

function tableCellSelectCB(~,evd)
    if evd.Indices(1) > 1
        try
            h.jtable.getCellEditor().stopCellEditing();
        catch
        end
    end
end