I am using the MATLAB 2014a uitable
and with the 'CellEditCallback'
, I create create a new figure by clicking a cell from my uitable. The problem is that the user may select multiple cells at the same time, then my program will open as much figures as the cells selected.
So I would like to know if it is possible to disable the uitable cell multiple selection. If not, do you have any suggestions to solve my issue ?
I'm aware this is 3 years old but I found a simple solution that worked for me that won't interfere with callbacks - and more importantly doesn't require callbacks to "de-select". I figured someone will benefit from this.
I'm using MATLAB2017a but the functionality to leverage is in the underlying JAVA object so should work with older versions (down to 2008).
You simply access the underlying Java table object and change the selection mode to SINGLE_SELECTION. For this we all need to thank Yair on his work on accessing the underlying Java table object and more importantly sharing it on the MATLAB file exchange (search for "findjobj" -- note the letter "J" in the middle!).
This method works weather you instantiated the MATLAB uitable through uitable
function or through implimenting it on guide
editor. You just have to pass in the handle to the matlab table object (note: there's a distinction between that and the underlying java table object!) into the above mentioned findjobj
function from MATLAB file exchange and configure the table in JAVA.
So the underlying JAVA feature we want to adjust is this
http://docs.oracle.com/javase/1.5.0/docs/api/javax/swing/JList.html#setSelectionMode(int)
Here's an example code that I verified using MATLAB 2017a on a 64 bit Windows machine:
% create a figure instance
h_fig = figure();
% Instantiate MATLAB's uitable
h_m_table = uitable( h_fig, ...
'Data', magic(3), ...
'ColumnName', {'A','B','C'} );
% if you already created a table using MATLAB's GUIDE editor, simply pass
% in the "tag" name property, which should be in the "handles" structure by
% default. If you didn't edit that field it's "uitable1" by default so:
%
% h_m_table = handles.uitable1 % replace 'uitable1' with tag name
% Get java scroll pane object
j_scrollpane = findjobj(h_m_table);
% Get java table object
j_table = j_scrollpane.getViewport.getView;
% (optional) Make entire ROW highlighted when user clicks on any row(s)
j_table.setNonContiguousCellSelection(false);
j_table.setColumnSelectionAllowed(false);
j_table.setRowSelectionAllowed(true);
% Set selction mode to SINGLE_SELECCTION
j_table.setSelectionMode(0);
Now you get a figure with a table on it and you can select only one row at a time by clicking.