JTable实现列自动适用内容宽度

12年前

    /*

     * 使表格自动适应内容调整列大小

     */

    public void fitTableColumns(JTable myTable){

 

         JTableHeader header = myTable.getTableHeader();

            int rowCount = myTable.getRowCount();

            Enumeration columns = myTable.getColumnModel().getColumns();

            while(columns.hasMoreElements()){

                TableColumn column = (TableColumn)columns.nextElement();

                int col = header.getColumnModel().getColumnIndex(column.getIdentifier());

                int width = (int)myTable.getTableHeader().getDefaultRenderer()

                        .getTableCellRendererComponent(myTable, column.getIdentifier()

                                , false, false, -1, col).getPreferredSize().getWidth();

                for(int row = 0; row<rowCount; row++){

                    int preferedWidth = (int)myTable.getCellRenderer(row, col).getTableCellRendererComponent(myTable,

                      myTable.getValueAt(row, col), false, false, row, col).getPreferredSize().getWidth();

                    width = Math.max(width, preferedWidth);

                }

                header.setResizingColumn(column); // 此行很重要

                column.setWidth(width+myTable.getIntercellSpacing().width + 4);//4,值目的是使表格看起来不是那么拥挤,起到间隔作用

            }

    }