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:
uitable: simply highlight, or change background color of certain cells

Subject: uitable: simply highlight, or change background color of certain cells

From: Camille Couzi

Date: 29 Sep, 2011 05:21:13

Message: 1 of 4

Hi matlabers!

I am dealing with a little problem...
I am working with an uitable which displays data for different files which are in a loop.
for example, in each file I look at: name, number of rows... so basically it is displaying information, and there are also editable columns where the user can choose some values for next processing on each file.

As you have guessed, I am including this uitable in a GUI, and the user can go through the files by pressing two buttons: "after" (file after) and "before" (file before).
When pressing one of these buttons, I would like to highlight in my uitable the columns related to that file. When I say highlight, I don't care if it is changing the background color of the cells, or changing the color font of the text inside these cells...

I have read lots of posts, where people speak about Java. I have no idea about Java. is there really any way to do that? or do you know about sbdy who perhaps posted something on the file exchange?

Thanks in advance for your help!
camille.

Subject: uitable: simply highlight, or change background color of certain cells

From: matt dash

Date: 29 Sep, 2011 14:56:28

Message: 2 of 4

"Camille Couzi" <cco@dhi.com.sg> wrote in message <j60v89$bmn$1@newscl01ah.mathworks.com>...
> Hi matlabers!
>
> I am dealing with a little problem...
> I am working with an uitable which displays data for different files which are in a loop.
> for example, in each file I look at: name, number of rows... so basically it is displaying information, and there are also editable columns where the user can choose some values for next processing on each file.
>
> As you have guessed, I am including this uitable in a GUI, and the user can go through the files by pressing two buttons: "after" (file after) and "before" (file before).
> When pressing one of these buttons, I would like to highlight in my uitable the columns related to that file. When I say highlight, I don't care if it is changing the background color of the cells, or changing the color font of the text inside these cells...
>
> I have read lots of posts, where people speak about Java. I have no idea about Java. is there really any way to do that? or do you know about sbdy who perhaps posted something on the file exchange?
>
> Thanks in advance for your help!
> camille.


What if pressing the buttons selected the desired cells? Is that ok or does it have to be a color change? (Selecting can be done with minimal java, recoloring will definitely require java)

Subject: uitable: simply highlight, or change background color of certain cells

From: Camille Couzi

Date: 30 Sep, 2011 06:00:27

Message: 3 of 4

Hi Matt,

Thanks for your reply.
What do you mean by "select"? I just want a simple way to show the user which file he is watching by highlighting or changing the colors of the background or the text of the cells.

Yesterday I was thinking that this could be possible only if there is a way to assign a name or handle to each cell of the uitable. If for example you have 4 cells in your table:
| cell1 | cell2 | cell3 | cell4 |

and you want to change the background color of cell2 and put ir red (knowing that its handle is h.cell2 for example), it would be very easy to write something like

set(h.cell2,'backgroundcolor','r')

Do you know what I mean? It's very strange that people from MathWorks think that this will be a very good add to uitable!!!

Thanks in advance for your suggestions...
Camille.

> What if pressing the buttons selected the desired cells? Is that ok or does it have to be a color change? (Selecting can be done with minimal java, recoloring will definitely require java)

Subject: uitable: simply highlight, or change background color of certain cells

From: matt dash

Date: 30 Sep, 2011 16:17:28

Message: 4 of 4

"Camille Couzi" <cco@dhi.com.sg> wrote in message <j63ltr$pgr$1@newscl01ah.mathworks.com>...
> Hi Matt,
>
> Thanks for your reply.
> What do you mean by "select"? I just want a simple way to show the user which file he is watching by highlighting or changing the colors of the background or the text of the cells.

I mean select like you would select a cell in a spreadsheet by clicking on it. There is no way to recolor specific cells in a table without writing some real java.

Here is a somewhat kludgey example of how you can programatically select cells. It begins by "manually" making a uitable using some underlying java commands. A real matlab uitable wouldnt use a defaulttablemodel, but I think the type of tablemodel it uses depends on your matlab version, so the defaultmodel is safer, but it provides fewer options... notably it will render all your cells like text (no check boxes etc). If you need those capabilities, it would be possible to use a more sophisticated table model.

The highlighting is accomplished by the last two lines of the code, which are the ones you would reuse in whatever function you need to programatically change the highlighting. Note that these are java commands, so they start counting rows/columns at 0 instead of 1, so you'll need to subtract 1 from your indices.

There is one slight problem though... they color the first cell of the selection differently from the others (same as Excel does). To avoid that, I add an additional column at the beginning of the table, and set its width to zero so that it doesn't actually appear in the table. So to do this you'd need to append a column of junk data to the beginning of your data matrix.




f=figure;
data=num2cell(rand(10,5)); %5 columns of data, but only the last 4 are visible.
names={'','A','B','C','D'}; %first name isnt used
mod=javax.swing.table.DefaultTableModel(data,names); %make a table model
peer=com.mathworks.hg.peer.UitablePeer(mod); %do some matlab stuff that puts the model in a table
table=peer.getTable; %get the table
jscroll=javax.swing.JScrollPane(table); %put the table in a scroll pane
u=javacomponent(jscroll,[30 30 300 300],f); %put the scroll pane in the figure

%hide first column
table.getColumnModel.getColumn(0).setMinWidth(0)
table.getColumnModel.getColumn(0).setPreferredWidth(0)

%select desired rows:
table.setRowSelectionInterval(3,4) %select rows 3 and 4
table.setColumnSelectionInterval(0,4) %select all columns

Tags for this Thread

No tags are associated with 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