/**调整表格列的宽度。*/ //1private void setTableHeaderWidth(){for(int i=0;i<table.getColumnCount();i++){TableColumn tc=table.getColumn(table.getColumnName(i));int width=getPreferredWidthForColumn(tc)+10;tc.setMinWidth(width);}}/**获取一个列的推荐宽度,返回列名和列取值的最大宽度。*/ //2private int getPreferredWidthForColumn(TableColumn col){int hw=columnHeaderWidth(col),cw=widestCellInColumn(col);return hwcw?hw:cw;}/**表头列的宽度*/ //3private int columnHeaderWidth(TableColumn col){TableCellRenderer renderer=col.getHeaderRenderer();if(renderer==null){renderer=table.getTableHeader().getDefaultRenderer();}if(renderer==null)return 0;Component comp=renderer.getTableCellRendererComponent(table,col.getHeaderValue(),false,false,0,0);return comp.getPreferredSize().width;}/**值列的最大宽度*/ //4
温馨提示:答案为网友推荐,仅供参考