Resize rowname column width of uitable in MATLAB

21 views (last 30 days)
The column with the rownames in uitable is excessively wide and I want to make it smaller. I attached example of my uitable with excessively wide of rawnames. In rownames I use symbols with using special inserting. So, is it possible to resize the rownames column of this uitable? I added part of my code below:
WAngles={0 180; 10 180; 0 180};
cnames = {'<HTML><font size=+1><center />Minimum<br/>Value</HTML>',...
'<HTML><font size=+1><center />Maximum<br />Value</HTML>'};
AngName={'<HTML><font size=+1>&alpha</HTML>';...
'<HTML><font size=+1>&beta</HTML>';...
'<HTML><font size=+1>&theta</HTML>'};
columneditable2 = [true true];
TabAngles=uitable('Data',WAngles,'Units', 'normalized','Position',...
[0.07 0.03 0.453 0.1555],'ColumnWidth',{100},'ColumnName',cnames,...
'RowName',AngName,'FontName','Book Antiqua','FontSize',12,...
'ColumnEditable', columneditable2,'Tag','TabMass');

Answers (1)

Charlie
Charlie on 22 Sep 2017
The following example includes a solution for this.
FontSize = 18;
fh = figure;
data = {1,1,1,1; 2,2,1,1; 3,6,1,1; 4,4,4,1; 6,5,4,1};
cnames = {'Speed (kph)','Curvature (m)','Banking (deg)','Elevation (m)'};
rnames = {'Braking','Entry','Mid Corner', 'Mid Throttle', 'Exit'};
t = uitable('Units','normalized','Position',[0 0 1 1],'Data',data,'ColumnName',cnames,'RowName',rnames,'FontSize',FontSize);
hs = '<html><font size="+2">'; %html start
he = '</font></html>'; %html end
cnh = cellfun(@(x)[hs x he],cnames,'uni',false); %with html
rnh = cellfun(@(x)[hs x he],rnames,'uni',false); %with html
set(t,'ColumnName',cnh,'RowName',rnh) %apply
%get the row header
jscroll=findjobj(t);
rowHeaderViewport=jscroll.getComponent(4);
rowHeader=rowHeaderViewport.getComponent(0);
height=rowHeader.getSize;
rowHeader.setSize(80,360)
%resize the row header
newWidth=150; %100 pixels.
rowHeaderViewport.setPreferredSize(java.awt.Dimension(newWidth,0));
height=rowHeader.getHeight;
rowHeader.setPreferredSize(java.awt.Dimension(newWidth,height));
rowHeader.setSize(newWidth,height);
figPos = get(fh,'Position');
tableExtent = get(t,'Extent');
set(fh,'Position',[figPos(1:2), figPos(3:4).*tableExtent(3:4)]);
  2 Comments
Terry Hasseman
Terry Hasseman on 7 Sep 2018
I am trying to edit properties of the row header object, too. However, I get an error when using this solution in Matlab 2017A.
rowHeaderViewport=jscroll.getComponent(4);
Returns the error:
Java exception occurred:
java.lang.ArrayIndexOutOfBoundsException: No such child: 4
at java.awt.Container.getComponent(Unknown Source)
It seems the jscroll object only has 3 children. Unless I manually open the object in the workspace. Then it suddenly returns as having six children and I can then use the .getComponent(4) function. Unfortunately, this is going to be used in a compiled application so there will be no option for the user to manually pause the code execution, open the object/variable manually in the workspace, re-run the findjobj() command, and finally run the .getComponent() function another time to be able to edit the properties of the row header.
Any suggestions on how I can make this work without all of the additional manual intervention?
michael fried
michael fried on 21 Mar 2024
Apparently you're getting the uitable-object itself. So in your example above, where t is a uitable: in order to get the rowHeaderViewport, do:
% using the try-catch, this code should work with older and newer versions
jscroll = findjobj(t);
try
rowHeaderViewport=jscroll.getComponent(4);
catch ME
x1 = jscroll;
x2 = x1.getParent();
jscroll = x2.getParent();
rowHeaderViewport = jscroll.getComponent(4);
end
%... etc...
Does this work for you?

Sign in to comment.

Categories

Find more on Migrate GUIDE Apps in Help Center and File Exchange

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!