Borrar filtros
Borrar filtros

Resize rowname column width of uitable in MATLAB

6 visualizaciones (últimos 30 días)
Grigoriy Yashin
Grigoriy Yashin el 14 de Oct. de 2016
Comentada: michael fried el 21 de Mzo. de 2024
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');

Respuestas (1)

Charlie
Charlie el 22 de Sept. de 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 comentarios
Terry Hasseman
Terry Hasseman el 7 de Sept. de 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 el 21 de Mzo. de 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?

Iniciar sesión para comentar.

Categorías

Más información sobre Migrate GUIDE Apps en Help Center y File Exchange.

Community Treasure Hunt

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

Start Hunting!

Translated by