Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.java.gui > #734

Re: how can i set center

Path csiph.com!x330-a1.tempe.blueboxinc.net!feeder1.hal-mli.net!news.glorb.com!news-out.readnews.com!transit3.readnews.com!news-out.news.tds.net!newsreading01.news.tds.net!86597e80!not-for-mail
From "Michael Rauscher" <michael.rauscher@THRWHITE.remove-dii-this>
Subject Re: how can i set center
Message-ID <eor3q6$eh4$1@registered.motzarella.org> (permalink)
X-Comment-To comp.lang.java.gui
Newsgroups comp.lang.java.gui
In-Reply-To <1169200491.118798.86410@s34g2000cwa.googlegroups.com>
References <1169200491.118798.86410@s34g2000cwa.googlegroups.com>
Content-Type text/plain; charset=IBM437
Content-Transfer-Encoding 8bit
X-Gateway time.synchro.net [Synchronet 3.15a-Win32 NewsLink 1.92]
Lines 52
Date Wed, 27 Apr 2011 15:29:15 GMT
NNTP-Posting-Host 96.60.20.240
X-Complaints-To news@tds.net
X-Trace newsreading01.news.tds.net 1303918155 96.60.20.240 (Wed, 27 Apr 2011 10:29:15 CDT)
NNTP-Posting-Date Wed, 27 Apr 2011 10:29:15 CDT
Organization TDS.net
Xref x330-a1.tempe.blueboxinc.net comp.lang.java.gui:734

Show key headers only | View raw


  To: comp.lang.java.gui
ashwinijain schrieb:
> Michael Rauscher wrote:
>> ashwinijain schrieb:
>>> hi, i am using a jtable for making some calculation and i want to set
>>> the center alignment of last column.
>>> But i really dont know how to set it?
>>> Please help me out...
>>> I used following method but it works for all columns..
>>> ((JLabel)tab1.getDefaultRenderer(String.class)).setHorizontalAlignment
>>> (JLabel.CENTER);
>>>
>>> And i want to set alignment for last column only.
>>>
>> There are different methods to do what you want to be done :)
>>
>> The most simple should be to override JTable#getCellRenderer, e. g.
>>
>> JTable myTable = new JTable(myModel) {
>>      public TableCellRenderer getCellRenderer( int row, int col ) {
>>          TableCellRenderer renderer = super.getCellRenderer(row,col);
>>          if ( col == dataModel.getColumnCount() - 1 )
>>              renderer.setHorizontalAlignment( SwingConstants.CENTER );
>>          return renderer;
>>      }
>> };
>>
>> Bye
>> Michael
> 
> Thanks but its giving me error that "setHorizontalAlignment(int) is
> undefined for renderer.

Sorry, I forgot to cast. Change the above to:

       TableCellRenderer renderer = super.getCellRenderer(row,col);

       int alignment = (col == dataModel.getColumnCount() - 1) ?
               SwingConstants.CENTER : SwingConstants.LEFT;

       ((JLabel)renderer).setHorizontalAlignment( alignment );
       return renderer;

Should work (but not tested).

Bye
Michael

---
 * Synchronet * The Whitehouse BBS --- whitehouse.hulds.com --- check it out free usenet!
--- Synchronet 3.15a-Win32 NewsLink 1.92
Time Warp of the Future BBS - telnet://time.synchro.net:24

Back to comp.lang.java.gui | Previous | NextNext in thread | Find similar


Thread

Re: how can i set center "Michael Rauscher" <michael.rauscher@THRWHITE.remove-dii-this> - 2011-04-27 15:29 +0000
  Re: how can i set center "ashwinijain" <ashwinijain@THRWHITE.remove-dii-this> - 2011-04-27 15:29 +0000
    Re: how can i set center "Michael Rauscher" <michael.rauscher@THRWHITE.remove-dii-this> - 2011-04-27 15:29 +0000

csiph-web