Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

row-wise selection in a uitable

1,466 views
Skip to first unread message

Adam

unread,
Feb 3, 2011, 6:54:04 PM2/3/11
to
I'm developing a matlab gui using R2010b. The gui has a uitable. By following the hints here (http://www.mathworks.com/matlabcentral/newsreader/view_thread/268448#702699) I have made clicks on the uitable select an entire row.

The code to do this is below:
hJScroll = findjobj(handles.tblParamsets); % findjobj is from file exchange
hJTable = hJScroll.getViewport.getView; % get the table component within the scroll object
hJTable.setNonContiguousCellSelection(false);
hJTable.setColumnSelectionAllowed(false);
hJTable.setRowSelectionAllowed(true);
set(handles.tblParamsets, 'CellSelectionCallback', '...'); % from GUIDE

The problem I'm having is that once I turn on the non-contiguous cell selection property of the Java object, selecting rows in the uitable no longer triggers the cell selection callback. If I don't set this parameter to false, the cell selection callback still fires, but I can't constrain the user to select entire rows of the table.

Has anyone solved this problem in the past? If so, how?

Really all that I want to happen is (a) for the user to be able to select entire rows of the table, and, (b) have a callback that'll tell me which row was selected.

Thanks,
Adam

Adam

unread,
Feb 22, 2011, 10:49:04 PM2/22/11
to
Hi all,

I figured this problem out myself a few weeks back but thought I'd share my solution just in case anyone else has the problem in the future.

I used one the callbacks defined for the JTable object as the matlab callback didn't seem to be triggering after I messed with the properties of the JTable object.

So I added something similar to this to my original code:
hJTable = handle(hJTable, 'CallbackProperties');
set(hJTable, 'MousePressedCallback', {@MyCellSelectionCallback, gcf});

...and then I defined my callback function...
function MyCellSelectionCallback(hObject, eventData, currFig)
% commands to so stuff on selection of a table row

coffee_pot

unread,
Mar 22, 2011, 10:08:04 PM3/22/11
to
Thanks. This came in really handy for me!

I also used tips from this thread: http://www.mathworks.com/matlabcentral/newsreader/view_thread/165066

In particular, the 6th message which says:

"> Is there a way to tell what cells are currently selected?

mtable = uitable(gcf, magic(3), {'A', 'B', 'C'});
jtable = mtable.getTable;
row = jtable.getSelectedRow + 1; % Java indexes start at 0
col = jtable.getSelectedColumn + 1;

If multiple cells are selected, you might want to use
getSelectedRows and getSelectedColumns which return an array
of int32 indexes (0-based).

Yair"

"Adam " <ad...@noneedforspam.abc> wrote in message <ik203g$nql$1...@fred.mathworks.com>...

Rebel_Longhorn

unread,
Apr 18, 2015, 12:53:07 PM4/18/15
to
Where does the main part of the code below (starting with hJScroll) go within the GUIDE framework? Is it in a callback?


"Adam" wrote in message <iiff6s$pl9$1...@fred.mathworks.com>...

Bruno Luong

unread,
Apr 19, 2015, 3:09:05 AM4/19/15
to
"Rebel_Longhorn" <han...@unlv.nevada.edu> wrote in message <mgu258$4iu$1...@newscl01ah.mathworks.com>...
> Where does the main part of the code below (starting with hJScroll) go within the GUIDE framework? Is it in a callback?

No, it when the uitable is invoked and after the table is creates.

Rebel_Longhorn

unread,
Apr 19, 2015, 10:13:06 PM4/19/15
to
So in my GUIDE block , I have this:

function uitable1_CreateFcn(hObject, eventdata, handles)
%[Default commented info]

hJScroll = findjobj(handles.tblParamsets); % findjobj is from file exchange
hJTable = hJScroll.getViewport.getView; % get the table component within the scroll object
hJTable.setNonContiguousCellSelection(false);
hJTable.setColumnSelectionAllowed(false);
hJTable.setRowSelectionAllowed(true);
set(handles.tblParamsets, 'CellSelectionCallback', '...');
hJTable = handle(hJTable, 'CallbackProperties');
set(hJTable, 'MousePressedCallback', {@MyCellSelectionCallback, gcf});



Is that the correct location? Also, if this is so, how can I index the data in the selected row? I.e., I want to select a row, then have the data from column 1 sent to one place, and column 3 to another.


"Bruno Luong" <b.l...@fogale.findmycountry> wrote in message <mgvkaa$nrf$1...@newscl01ah.mathworks.com>...

Bruno Luong

unread,
Apr 20, 2015, 5:57:10 PM4/20/15
to
> Is that the correct location? Also, if this is so, how can I index the data in the selected row? I.e., I want to select a row, then have the data from column 1 sent to one place, and column 3 to another


function MyCellSelectionCallback(juiTable, varargin)
row = get(juiTable,'SelectedRow')+1;
% ...
end

Rebel_Longhorn

unread,
Apr 23, 2015, 12:32:17 AM4/23/15
to
Thanks you for the help, but I'm still missing something. My GUI worked fine before I asked about this topic. Since then, I have added this:

function uitable1_CreateFcn(hObject, eventdata, handles)
hJScroll = findjobj(handles.tblParamsets);
hJTable = hJScroll.getViewport.getView;
hJTable.setNonContiguousCellSelection(false);
hJTable.setColumnSelectionAllowed(false);
hJTable.setRowSelectionAllowed(true);
set(handles.tblParamsets, 'CellSelectionCallback', '...');
hJTable = handle(hJTable, 'CallbackProperties');
set(hJTable, 'MousePressedCallback', {@uitable_CellSelectionCallback, gcf});

then later:

function uitable1_CellSelectionCallback(juiTable, varargin)
row = get(juiTable,'SelectedRow')+1;

Now I get these errors when I try to run it:

Attempt to reference field of non-structure array.
Error in GUI_2>uitable1_CreateFcn (line 386)
hJScroll = findjobj(handles.tblParamsets); % findjobj is from file exchange
Error in gui_mainfcn (line 95) %this is from earlier in my code, worked fine before the addition
feval(varargin{:});
Error in GUI_2 (line 42) %this is from earlier in my code, worked fine before the addition
gui_mainfcn(gui_State, varargin{:});
Error in @(hObject,eventdata)GUI_2('uitable1_CreateFcn',hObject,eventdata,guidata(hObject))
Error using struct2handle
Error while evaluating uitable CreateFcn

I'm so new at this side of things, I have no idea what I'm doing wrong. Again, in uitable1 I want to select a whole row with 1 click, and with that same click have the values in Col 1 and Col 3 be sent to fields elsewhere in the GUI.

"Bruno Luong" <b.l...@fogale.findmycountry> wrote in message <mh3snf$iai$1...@newscl01ah.mathworks.com>...

Bruno Luong

unread,
Apr 23, 2015, 7:21:22 AM4/23/15
to
"Rebel_Longhorn" <han...@unlv.nevada.edu> wrote in message
> hJScroll = findjobj(handles.tblParamsets); % findjobj is from file exchange

Who set the value of handles.tblParamsets? It should contain of your uitable.

Bruno

Rebel_Longhorn

unread,
Apr 24, 2015, 12:06:19 AM4/24/15
to
handles.tblParamsets is not set anywhere, I hadn't noticed that. Where's it supposed to be, and what does it get set to? I created the base layout using the GUIDE visual editor, so I did not code this whole framework from scratch. That method did not create a handles.tblParamsets so I have no idea what it should look like or where is should appear.

Thanks so much for you time so far and I apologize for any rookie mistakes. I can also email the whole thing to you to look at if it would be easier.


"Bruno Luong" <b.l...@fogale.findmycountry> wrote in message <mhakjd$2kc$1...@newscl01ah.mathworks.com>...

Bruno Luong

unread,
Apr 24, 2015, 12:20:25 PM4/24/15
to
"Rebel_Longhorn" <han...@unlv.nevada.edu> wrote in message <mhcffm$5sm$1...@newscl01ah.mathworks.com>...
> handles.tblParamsets is not set anywhere, I hadn't noticed that. Where's it supposed to be, and what does it get set to? I created the base layout using the GUIDE visual editor, so I did not code this whole framework from scratch.

Then handle of the uitable is input argument "hObject" of the Create function.
0 new messages