Docjar: A Java Source and Docuemnt Enginecom.*    java.*    javax.*    org.*    all    new    plug-in

Quick Search    Search Deep

Source code: com/yaftp/utils/SwingTableColumnSizer.java


1    /**
2    *
3    * CopyRights Jean-Yves MENGANT 1999,2000,2001,2002
4    *
5    * This program is free software; you can redistribute it and/or
6    * modify it under the terms of the GNU General Public License
7    * as published by the Free Software Foundation; either version 2
8    * of the License, or any later version.
9    *
10   * This program is distributed in the hope that it will be useful,
11   * but WITHOUT ANY WARRANTY; without even the implied warranty of
12   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13   * GNU General Public License for more details.
14   *
15   * You should have received a copy of the GNU General Public License
16   * along with this program; if not, write to the Free Software
17   * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
18   */
19  
20  package com.yaftp.utils ;
21  
22  import java.awt.* ;
23  import javax.swing.* ;
24  import javax.swing.table.* ;
25  
26  /**
27  
28    this class provides a generic way to size a given table columns
29    depending on its largest current content
30  
31    @Author Jean-Yves MENGANT
32    CopyRights Jean-Yves MENGANT 1998,1999,2000
33  
34  */
35  
36  public class SwingTableColumnSizer {
37  
38    private final static int NOGAP = -1 ;
39  
40    private JTable _table ;
41  
42  
43    public SwingTableColumnSizer( JTable table )
44    { _table = table ; }
45  
46  
47    private int columnHeaderWidth( TableColumn col )
48    {
49    TableCellRenderer renderer = col.getHeaderRenderer() ;
50  
51    // JDK 1.3 ONLY => uncomment to avoid null pointer exception
52    // if uncommented this code can be compiled by a JDK 1.3  javac compiler only
53    // but executed by both JRE 1.2 or JRE 1.3 java VM environment
54    if ( renderer == null )
55      renderer = _table.getTableHeader().getDefaultRenderer() ;
56  
57    Object headerValue = col.getHeaderValue() ;
58    Component component = renderer.getTableCellRendererComponent(
59                                                    _table , col.getHeaderValue() ,
60                                                    false , false , 0 , 0
61                                                                ) ;
62      return component.getPreferredSize().width ;
63    }
64  
65    private int widestCellInColumn( TableColumn col )
66    {
67    int c = col.getModelIndex() ;
68    int width = 0 ;
69    int maxw = 0  ;
70      for ( int r=0 ; r < _table.getRowCount() ;  ++r )
71      {
72      TableCellRenderer renderer = _table.getCellRenderer(r,c) ;
73  
74      Component component = renderer.getTableCellRendererComponent(
75                                                     _table , _table.getValueAt(r,c) ,
76                                                     false , false , r , c
77                                                                  )  ;
78        width = component.getPreferredSize().width ;
79        maxw = width > maxw ? width : maxw ;
80      }
81      return maxw ;
82    }
83  
84    private int getPreferredWidthForColumn( TableColumn col )
85    {
86      int hw = columnHeaderWidth( col ) ;
87      int cw = widestCellInColumn( col ) ;
88      return hw > cw ? hw : cw ;
89    }
90  
91    /**
92      calculate the preferred column size and optionnally add a pixel filler
93      if requested
94    */
95    public void resizeColumn( String name , int gap )
96    {
97    TableColumn column = _table.getColumn( name ) ;
98      int preferredWidth = getPreferredWidthForColumn( column ) ;
99      if ( gap != NOGAP )
100       preferredWidth += gap ;
101     column.setMaxWidth( preferredWidth );
102     column.setMinWidth( preferredWidth );
103     // if sizeColumnsToFit is not called repaint fails ...
104     // this is due to a Swing bug in JDK1.2.2
105     _table.sizeColumnsToFit(0) ;
106   }
107 }