Discover MakerZone

MATLAB and Simulink resources for Arduino, LEGO, and Raspberry Pi

Learn more

Discover what MATLAB® can do for your career.

Opportunities for recent engineering grads.

Apply Today

Thread Subject:
Changing Multiple Cells in Uitable

Subject: Changing Multiple Cells in Uitable

From: Anton Kotelyanskii

Date: 11 Jun, 2010 17:35:21

Message: 1 of 11

I'm wondering if there's a way to change data in multiple cells without having to reload the whole table?

I currently have a table with one column as checkboxes, and certain rows are used to represent a group of other rows. I currently have it so that if one of these "group" checkboxes is pressed, it changes the data for the table in those cells, then i use the set command to load the new data. This can be cumbersome and slow, so I am wondering if I can have the checkboxes changed without having to reload the whole data.

My current idea is to find a way to specifically refrence those cells, but I can't find a way to do that. I have found a way on the forums in which you go through the Java handle, but that doesn't work with 2010a.

Thanks

Subject: Changing Multiple Cells in Uitable

From: Walter Roberson

Date: 11 Jun, 2010 18:11:01

Message: 2 of 11

Anton Kotelyanskii wrote:
> I'm wondering if there's a way to change data in multiple cells without
> having to reload the whole table?

http://www.mathworks.com/matlabcentral/newsreader/view_thread/281187

Subject: Changing Multiple Cells in Uitable

From: Anton Kotelyanskii

Date: 12 Jun, 2010 15:59:07

Message: 3 of 11

Walter Roberson <roberson@hushmail.com> wrote in message <hutuav$lim$1@canopus.cc.umanitoba.ca>...
> Anton Kotelyanskii wrote:
> > I'm wondering if there's a way to change data in multiple cells without
> > having to reload the whole table?
>
> http://www.mathworks.com/matlabcentral/newsreader/view_thread/281187
This is the method that I found using Java handles that doesn't work with 2010a.
 jtable = mtable.getTable; results in the error message: "??? Attempt to reference field of non-structure array."

Subject: Changing Multiple Cells in Uitable

From: Yair Altman

Date: 12 Jun, 2010 20:19:07

Message: 4 of 11

"Anton Kotelyanskii" <akotelyanskiI@yahoo.com> wrote in message <hv0asb$k75$1@fred.mathworks.com>...
> Walter Roberson <roberson@hushmail.com> wrote in message <hutuav$lim$1@canopus.cc.umanitoba.ca>...
> > Anton Kotelyanskii wrote:
> > > I'm wondering if there's a way to change data in multiple cells without
> > > having to reload the whole table?
> >
> > http://www.mathworks.com/matlabcentral/newsreader/view_thread/281187
> This is the method that I found using Java handles that doesn't work with 2010a.
> jtable = mtable.getTable; results in the error message: "??? Attempt to reference field of non-structure array."

In your case, you are probably using the new (R2008a+, supported) version of uitable, rather than the old (pre-R2008a, unsupported) version. Unfortunately, unlike the unsupported version, the new version does not directly expose its Java handle. However, you can use the FindJObj utility on the File Exchange ( http://www.mathworks.com/matlabcentral/fileexchange/14317-findjobj ) to get this reference:

mtable = uitable(...);
jscroll = findjobj(mtable);
jtable = jscroll.getViewport.getComponent(0);

Note that the new uitable version extends com.jidesoft.grid.SortableTable, which provides the ability to group rows - something that could be most useful from your description of your problem. Obviously this functionality is only accessible via Java.

Yair Altman
http://UndocumentedMatlab.com

Subject: Changing Multiple Cells in Uitable

From: Felipe

Date: 7 Sep, 2010 20:48:09

Message: 5 of 11

"Yair Altman" <altmanyDEL@gmailDEL.comDEL> wrote in message <hv0q3r$qb3$1@fred.mathworks.com>...
> "Anton Kotelyanskii" <akotelyanskiI@yahoo.com> wrote in message <hv0asb$k75$1@fred.mathworks.com>...
> > Walter Roberson <roberson@hushmail.com> wrote in message <hutuav$lim$1@canopus.cc.umanitoba.ca>...
> > > Anton Kotelyanskii wrote:
> > > > I'm wondering if there's a way to change data in multiple cells without
> > > > having to reload the whole table?
> > >
> > > http://www.mathworks.com/matlabcentral/newsreader/view_thread/281187
> > This is the method that I found using Java handles that doesn't work with 2010a.
> > jtable = mtable.getTable; results in the error message: "??? Attempt to reference field of non-structure array."
>
> In your case, you are probably using the new (R2008a+, supported) version of uitable, rather than the old (pre-R2008a, unsupported) version. Unfortunately, unlike the unsupported version, the new version does not directly expose its Java handle. However, you can use the FindJObj utility on the File Exchange ( http://www.mathworks.com/matlabcentral/fileexchange/14317-findjobj ) to get this reference:
>
> mtable = uitable(...);
> jscroll = findjobj(mtable);
> jtable = jscroll.getViewport.getComponent(0);
>
> Note that the new uitable version extends com.jidesoft.grid.SortableTable, which provides the ability to group rows - something that could be most useful from your description of your problem. Obviously this functionality is only accessible via Java.
>
> Yair Altman
> http://UndocumentedMatlab.com

Yair:
   thanks for the usefull posts.
I can successfully get the java handle using finjobj like your coude suggests, however wen I change a property of the java object (jtable) the uitable doesen't change at all, no matter what changes I perform. I think that might not be the correct Java handle. I'm usig Matlab 2010a, I would greatly appreciate your help.
Regards

Subject: Changing Multiple Cells in Uitable

From: Yair Altman

Date: 8 Sep, 2010 07:02:07

Message: 6 of 11

"Felipe " <frlastrico@gmail.com> wrote in message
> I can successfully get the java handle using finjobj like your coude suggests, however wen I change a property of the java object (jtable) the uitable doesen't change at all, no matter what changes I perform. I think that might not be the correct Java handle. I'm usig Matlab 2010a, I would greatly appreciate your help.


If you posted your code snippet, I could see what you tried to do and possibly correct you

Yair

Subject: Changing Multiple Cells in Uitable

From: Felipe

Date: 8 Sep, 2010 18:26:19

Message: 7 of 11

"Yair Altman" <altmanyDEL@gmailDEL.comDEL> wrote in message <i67cdf$rgc$1@fred.mathworks.com>...
> "Felipe " <frlastrico@gmail.com> wrote in message
> > I can successfully get the java handle using finjobj like your coude suggests, however wen I change a property of the java object (jtable) the uitable doesen't change at all, no matter what changes I perform. I think that might not be the correct Java handle. I'm usig Matlab 2010a, I would greatly appreciate your help.
>
>
> If you posted your code snippet, I could see what you tried to do and possibly correct you
>
> Yair

I worked around a little bit. I wanted to do two things;
one was slecting the entire row when a user clicked on any cell belonging to that row. I was able to do this, I'll post the code:

%I created the java handle using the form
Table=uitable('Data',MyData,'CellSelectionCallback',@cellSelTable);
jtscroll=findjobj(Table);
jTable=jtscroll.getViewport.getComponent(0);

%then the cellSelTable callback funcion
function cellSelTable(ho,edata)
global jTable
persistent ctrlj
if isempty(ctrlj)
    ctrlj=0;
    XY=edata.Indices;
    jTable.changeSelection(XY(1)-1,0, false, false)
    jTable.changeSelection(XY(1)-1,3, false, true)
    return
end
ctrlj=ctrlj+1;
if ctrlj==2
    ctrlj=[];
end
end

I had to create the persistent variable "ctrlj" because when I call changeSelection it calls cellSelTable and without that solution it generated a loop. I know the solution may be very inefficient, but at least this does the trick and I hope it results useful to someone.

The second thing I want is to change the color of specific cells/rows acording to their values.
I am able to change the color of selected cells using the form
jTable.setSelectionForeground(java.awt.Color.blue)
jTable.setSelectionBackground(java.awt.Color.green)
but I can't figure out how to change the color of specific non selected cells.
I read about your java user-class ColoredFieldCellRenderer.java. But I don't know how to apply this to matlab. My biggest issue is my null knowledge of Java.
Thanks for your reply.

Subject: Changing Multiple Cells in Uitable

From: Michael

Date: 18 Sep, 2010 10:08:05

Message: 8 of 11

Hello!

I have nearly the same problem!
I just want to change the color of a specific cell in a table (independ of the value which is currently displayed). I am using 2010a and it seems a little bit tricky!? A code snippet would be great :-)


Best regards,
Michael

Subject: Changing Multiple Cells in Uitable

From: Yair Altman

Date: 18 Sep, 2010 18:04:05

Message: 9 of 11

"Michael " <xyMueller@web.de> wrote in message <i72325$7tr$1@fred.mathworks.com>...
> Hello!
>
> I have nearly the same problem!
> I just want to change the color of a specific cell in a table (independ of the value which is currently displayed). I am using 2010a and it seems a little bit tricky!? A code snippet would be great :-)


Simply place HTML formatting in your cell:
cellStr = '<html><font color="red">123.456';

See related: http://undocumentedmatlab.com/blog/html-support-in-matlab-uicomponents/

Yair Altman
http://UndocumentedMatlab.com

Subject: Changing Multiple Cells in Uitable

From: Michael

Date: 19 Sep, 2010 13:18:04

Message: 10 of 11

Yippi :-)
That's great and exactly what I needed!
Many, many thanks Mr. Altman!


Best regards,
Michael

Subject: Changing Multiple Cells in Uitable

From: Anton Kotelyanskii

Date: 15 Aug, 2012 18:59:11

Message: 11 of 11

"Michael" wrote in message <i752ic$gmk$1@fred.mathworks.com>...
> Yippi :-)
> That's great and exactly what I needed!
> Many, many thanks Mr. Altman!
>
>
> Best regards,
> Michael

Wow. So I'm coming back to this post more than a year later and I'm still dealing with jtable...

First of all, thanks a LOT to Mr. Altman, his various posts and articles have helped me a lot.

Second, I did find the solution to this problem a while ago. To use the various java/jtable commands, you have to summon the old version of uitable, which can be done by using the 'ColumnNames' property instead of 'ColumnName' when creating the table.

Tags for this Thread

What are tags?

A tag is like a keyword or category label associated with each thread. Tags make it easier for you to find threads of interest.

Anyone can tag a thread. Tags are public and visible to everyone.

Contact us