JTable调整列宽
生活随笔
收集整理的這篇文章主要介紹了
JTable调整列宽
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
1。// ? 取得列幅的最大值
? ? private ? int ? getPreferredWidthForCloumn(JTable ? table,int ? icol){
? ? ? ? TableColumnModel ? tcl ? = ? table.getColumnModel();
? ? ? ? TableColumn ? col ? = ? tcl.getColumn(icol);
? ? ? ? int ? c ? = ? col.getModelIndex(),width ? = ? 0,maxw ? = ? 0;
? ? ? ? for(int ? r=0;r <table.getRowCount();++r){
? ? ? ? ? ? TableCellRenderer ? renderer ? = ? table.getCellRenderer(r,c);
? ? ? ? ? ? Component ? comp ? = ? renderer.getTableCellRendererComponent(table,table.getValueAt(r,c),false,false,r,c);
? ? ? ? ? ? width ? = ? comp.getPreferredSize().width;
? ? ? ? ? ? maxw ? = ? width ? > ? maxw?width:maxw;
? ? ? ? }
2。自動設定列的寬度
? ? ? ? // 設定每列的寬度為當列的最大的寬度。
? ? ? ? for(int ? i= ? 0; ? i <table.getColumnCount(); ? i++){
? ? ? ? ? ? int ? with ? = ? this.getPreferredWidthForCloumn(table,i) ? + ? 10;
? ? ? ? ? ? with ? = ? iniCW[i] ? > ? with ? ? ? iniCW[i] ? : ? with;
? ? ? ? ? ? table.getColumnModel().getColumn(i).setPreferredWidth(with);
? ? ? ? }
? ? private ? int ? getPreferredWidthForCloumn(JTable ? table,int ? icol){
? ? ? ? TableColumnModel ? tcl ? = ? table.getColumnModel();
? ? ? ? TableColumn ? col ? = ? tcl.getColumn(icol);
? ? ? ? int ? c ? = ? col.getModelIndex(),width ? = ? 0,maxw ? = ? 0;
? ? ? ? for(int ? r=0;r <table.getRowCount();++r){
? ? ? ? ? ? TableCellRenderer ? renderer ? = ? table.getCellRenderer(r,c);
? ? ? ? ? ? Component ? comp ? = ? renderer.getTableCellRendererComponent(table,table.getValueAt(r,c),false,false,r,c);
? ? ? ? ? ? width ? = ? comp.getPreferredSize().width;
? ? ? ? ? ? maxw ? = ? width ? > ? maxw?width:maxw;
? ? ? ? }
2。自動設定列的寬度
? ? ? ? // 設定每列的寬度為當列的最大的寬度。
? ? ? ? for(int ? i= ? 0; ? i <table.getColumnCount(); ? i++){
? ? ? ? ? ? int ? with ? = ? this.getPreferredWidthForCloumn(table,i) ? + ? 10;
? ? ? ? ? ? with ? = ? iniCW[i] ? > ? with ? ? ? iniCW[i] ? : ? with;
? ? ? ? ? ? table.getColumnModel().getColumn(i).setPreferredWidth(with);
? ? ? ? }
轉載于:https://www.cnblogs.com/diyunpeng/archive/2011/12/28/2304932.html
總結
以上是生活随笔為你收集整理的JTable调整列宽的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 分享HTML 5的参考手册,演讲稿,电子
- 下一篇: BS的分析