Discover MakerZone

MATLAB and Simulink resources for Arduino, LEGO, and Raspberry Pi

Learn more

Discover what MATLAB® can do for your career.

Opportunities for recent engineering grads.

Apply Today

Thread Subject:
uitable center align data

Subject: uitable center align data

From: Anthony

Date: 15 Jun, 2011 15:47:04

Message: 1 of 6

Is there a way to center the data in a uitable, maybe through the underlying Java?

Subject: uitable center align data

From: Anthony

Date: 17 Jun, 2011 12:10:05

Message: 2 of 6

"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.

Subject: uitable center align data

From: Olivia Yang

Date: 3 Oct, 2011 16:58:27

Message: 3 of 6

"Anthony" wrote in message <itfg6t$sfk$1@newscl01ah.mathworks.com>...
> "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.

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

Subject: uitable center align data

From: David

Date: 4 Oct, 2012 18:39:08

Message: 4 of 6

"Olivia Yang" wrote in message <j6cpjj$7gf$1@newscl01ah.mathworks.com>...
> "Anthony" wrote in message <itfg6t$sfk$1@newscl01ah.mathworks.com>...
> > "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.
>
> 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

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?

Subject: uitable center align data

From: Yair Altman

Date: 3 Jan, 2013 18:51:08

Message: 5 of 6

"David" wrote in message <k4kl4c$7to$1@newscl01ah.mathworks.com>...
> "Olivia Yang" wrote in message <j6cpjj$7gf$1@newscl01ah.mathworks.com>...
> > "Anthony" wrote in message <itfg6t$sfk$1@newscl01ah.mathworks.com>...
> > > "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.
> >
> > 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
>
> 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?

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
 

Subject: uitable center align data

From: Matt S

Date: 30 Sep, 2014 15:59:09

Message: 6 of 6

"Yair Altman" wrote in message <kc4jus$qga$1@newscl01ah.mathworks.com>...
> "David" wrote in message <k4kl4c$7to$1@newscl01ah.mathworks.com>...
> > "Olivia Yang" wrote in message <j6cpjj$7gf$1@newscl01ah.mathworks.com>...
> > > "Anthony" wrote in message <itfg6t$sfk$1@newscl01ah.mathworks.com>...
> > > > "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.
> > >
> > > 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
> >
> > 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?
>
> 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
>

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

Tags for this Thread

What are tags?

A tag is like a keyword or category label associated with each thread. Tags make it easier for you to find threads of interest.

Anyone can tag a thread. Tags are public and visible to everyone.

Contact us