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

Change the row names of table from createTable

161 views
Skip to first unread message

Bruno Luong

unread,
Nov 23, 2008, 3:15:04 PM11/23/08
to
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

Yair Altman

unread,
Nov 24, 2008, 8:08:02 AM11/24/08
to
"Bruno Luong" <b.l...@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

Bruno Luong

unread,
Nov 24, 2008, 12:50:27 PM11/24/08
to
Hello Yair,

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

regards,

Bruno


Rob Ewalds

unread,
Feb 9, 2011, 8:29:03 AM2/9/11
to
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);

0 new messages