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


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

Re: how can i set center

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)
Newsgroups comp.lang.java.gui
References <1169200491.118798.86410@s34g2000cwa.googlegroups.com>
Date 2011-04-27 15:29 +0000
Organization TDS.net

Show all headers | 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