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
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
Your reply surely is an insight for me and I could manage to get what I wanted. Much thanks.
regards,
Bruno
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);