Quantcast
Channel: MATLAB Central Newsreader - uitable center align data
Viewing all articles
Browse latest Browse all 9

Re: uitable center align data

$
0
0
"Matt S" wrote in message <m0ek0d$a44$1@newscl01ah.mathworks.com>...
> "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

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

Viewing all articles
Browse latest Browse all 9

Latest Images

Trending Articles





Latest Images