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

uitable center align data

1,137 views
Skip to first unread message

Anthony

unread,
Jun 15, 2011, 11:47:04 AM6/15/11
to
Is there a way to center the data in a uitable, maybe through the underlying Java?

Anthony

unread,
Jun 17, 2011, 8:10:05 AM6/17/11
to
"Anthony" wrote in message <itak5o$504$1...@newscl01ah.mathworks.com>...

> Is there a way to center the data in a uitable, maybe through the underlying Java?
I've been trying through the JTable interface, this is what I have so far:

table = findjobj(handles.cutTable); %findjobj is in the file exchange
table1 = get(table,'Viewport');
jtable = get(table1,'View');
renderer = jtable.getCellRenderer(2,2);
renderer.setHorizontalTextPosition(javax.swing.swingConstants.CENTER);

This doesn't throw any errors, but it also doesn't seem to change anything. I eventually want to set all the cells to center. What am I doing wrong? My Java isn't good, and I've never tried doing anything with the underlying Java in Matlab before.

Olivia Yang

unread,
Oct 3, 2011, 12:58:27 PM10/3/11
to
"Anthony" wrote in message <itfg6t$sfk$1...@newscl01ah.mathworks.com>...
It works if I set both text and alignment together:

table = findjobj(handles.cutTable); %findjobj is in the file exchange
table1 = get(table,'Viewport');
jtable = get(table1,'View');
renderer = jtable.getCellRenderer(2,2);
renderer.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);
renderer.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);

Suwen

David

unread,
Oct 4, 2012, 2:39:08 PM10/4/12
to
"Olivia Yang" wrote in message <j6cpjj$7gf$1...@newscl01ah.mathworks.com>...
I used the above code by Suwen (but I substituted handles.cutTable for the handle of the uitable I created) but I wasn't able to get the text to center. Does anyone have any hints on how I can get the code to center text in my uitable?

Yair Altman

unread,
Jan 3, 2013, 1:51:08 PM1/3/13
to
"David" wrote in message <k4kl4c$7to$1...@newscl01ah.mathworks.com>...
Java table cell renderers are only effective on the old (pre-R2008a) uitable; in the new uitable, they get overridden by the data model (which is a big no-no BTW, the mathworker who did this needs to go back to school). I explain all this (plus how to fix it) in my uitable customization report:
http://UndocumentedMatlab.com/blog/uitable-customization-report/

The basic idea is to replace the new uitable's data model with a DefaultTableModel, after which renderers will start working.

Yair Altman
http://UndocumentedMatlab.com

Matt S

unread,
Sep 30, 2014, 11:59:09 AM9/30/14
to
"Yair Altman" wrote in message <kc4jus$qga$1...@newscl01ah.mathworks.com>...
I found something that seems to work without changing the data model. I got this to work in Matlab 2012a. It centers the text for all the cells.

figure;tableHandle = uitable;
set(tableHandle,'Data',rand(5,2))

jscrollpane = findjobj(tableHandle);
jTable = jscrollpane.getViewport.getView;

cellStyle = jTable.getCellStyleAt(0,0);
cellStyle.setHorizontalAlignment(cellStyle.CENTER);

% Table must be redrawn for the change to take affect
jTable.repaint;

Matt

-unknown

unread,
Oct 3, 2016, 10:17:08 AM10/3/16
to
"Matt S" wrote in message <m0ek0d$a44$1...@newscl01ah.mathworks.com>...
I want something similar but only change certain rows/columns, is that possible?

Yair Altman

unread,
Oct 4, 2016, 12:18:09 PM10/4/16
to
"-unknown" wrote in message <nstp8v$9nj$1...@newscl01ah.mathworks.com>...

> I want something similar but only change certain rows/columns, is that possible?


Yes: http://undocumentedmatlab.com/blog/aligning-uicontrol-contents

Yair Altman
http://UndocumentedMatlab.com

-unknown

unread,
Oct 12, 2016, 9:24:11 AM10/12/16
to
"Yair Altman" wrote in message <nt0knr$o5o$1...@newscl01ah.mathworks.com>...
Thanks for replying. Unfortunantly, im a bit confused: im using guide/java (like the code above) so changing my already made code into html is not desirable. Is something similar possible via java as above? Something like:

cellStyle = jTable.getCellStyleAt(1,1);
cellStyle.setHorizontalAlignment.Range(1:3).(cellStyle.LEFT);
cellStyle.setHorizontalAlignment.Range(4:10).(cellStyle.RIGHT);

Thanks

cc.ca...@gmail.com

unread,
Apr 15, 2019, 5:01:13 AM4/15/19
to
Works beautifully in R2018a as well. Cheers! Much neater solution than html and associated parsing.
0 new messages