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:
Change the row names of table from createTable

Subject: Change the row names of table from createTable

From: Bruno Luong

Date: 23 Nov, 2008 20:15:04

Message: 1 of 4

I'm using the createTable from Yair Altman to display and edit numerical data (I take this opportunity to thank Yair). Before you ask, for compatibility issue I can't use the official uitable.

The rows are by default named as '1', '2', ... Is there any way to delete the row names in the left column or change the row names to something else?

Thanks,

Bruno

Subject: Change the row names of table from createTable

From: Yair Altman

Date: 24 Nov, 2008 13:08:02

Message: 2 of 4

"Bruno Luong" <b.luong@fogale.findmycountry> wrote in message <ggcdk7$eov$1@fred.mathworks.com>...
> I'm using the createTable from Yair Altman to display and edit numerical data (I take this opportunity to thank Yair). Before you ask, for compatibility issue I can't use the official uitable.
>
> The rows are by default named as '1', '2', ... Is there any way to delete the row names in the left column or change the row names to something else?


Hi Bruno,

Firstly, createTable uses uitable so I'm not sure how exactly it solves your compatibility issue. In any case, modifying the row headers is very tricky. This is because the row headers are actually not part of the main table but a separate 1-column table that is linked to the main table and is part of the containing SpreadsheetScrollPane - this is actually standard practice in the Java community (uitable is basically a Java object). If this is a bit difficult to visualize, then I suggest you use my FindJObj submission on the file Exchange (http://www.mathworks.com/matlabcentral/fileexchange/14317) to see a hierarchical view of the table (including its viewports, scroolbars and column headers). It gets complicated because Matlab's default row headers table do not allow modifying the header labels...

So here are two simple hacks (these hacks also work for the Java uitable if anyone is interested). None of them is perfect so if anyone finds some better solution please post a reply on this post.

Hack #1: replace the header table with our own header table
  => notes: will not be automatically updated when rows are added/deleted (needs extra table listeners); does not allow row selection

table = createTable(gcf,{'a','b','c','d'},magic(4));
tableHeaderViewport = table.getTableScrollPane.getComponent(3);
tableHeaderViewport.remove(0);
tableHeaderViewport.add(javax.swing.JTable({'a1';'b2';'c3;d4'},'xy'));

remember to update the header width:

size = tableHeaderViewport.getPreferredSize;
size.setWidth(30); % #pixels
tableHeaderViewport.getPreferredSize(size);
tableHeaderViewport.revalidate;

Hack #2: keep Matlab's header table, but just replace its TableModel:
  => notes: will not be automatically updated when rows are added/deleted (needs extra table listeners); allows row selection but reverts back to default (1,2,...) labels

table = createTable(gcf,{'a','b','c','d'},magic(4));
tableHeaderViewport = table.getTableScrollPane.getComponent(3);
tableHeader = tableHeaderViewport.getComponent(0);
newModel = javax.swing.table.DefaultTableModel({'a1';'b2';'c3;d4'},'xy'));
tableHeader.setModel(newModel);
% update the header width as above

Yair altman

Subject: Change the row names of table from createTable

From: Bruno Luong

Date: 24 Nov, 2008 17:50:27

Message: 3 of 4

Hello Yair,

Your reply surely is an insight for me and I could manage to get what I wanted. Much thanks.

regards,

Bruno

Subject: Change the row names of table from createTable

From: Rob Ewalds

Date: 9 Feb, 2011 13:29:03

Message: 4 of 4

Hi Yair,

First I would like to take the opportunity to express my gratitude for all your "undocumented matlab" posts. With respect to GUI design, you have done much of the work that the MathWorks should have been doing over the last years...
You helped me out a great deal!

I found a way to neatly modify the names of the 'rowHeader', please have a look at this example. It works for me:

% Create an exemplary table
clear all; clc;
fig = figure;
table = uitable(fig, rand(4), {'A', 'B', 'C', 'D'});

% Implement new 'rowHeader' in 'scrollPane'
jtable = table.getTable;
listModel = {'s1', 's2', 's3', 's4'};
rowHeader = javax.swing.JList(listModel);
scrollPane = table.getTableScrollPane;
scrollPane.setRowHeaderView(rowHeader);

% Set background color, header cell height- & width
rowHeader.setBackground(scrollPane.getBackground);
rowHeader.setFixedCellHeight(jtable.getRowHeight - jtable.getRowMargin);
rowHeader.setFixedCellWidth(20);

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