使用setPreferredWidth方法即可,给你个例子,根据不同的表名(此参数按你需求可改),动态改变表的列宽:
import javax.swing.*;
import javax.swing.table.*;
/**
* 作者:alphazhao
* 日期:2002-5-25
* 描述:设定列表显示的各列列宽及其它设定,进入参数为tablename和JTable
* */
public class TableColumnSet {
public TableColumnSet(String tableName,JTable tableModel) {
tcm = tableModel.getColumnModel();
if(tableName.equals("UserLog")) {//用户
int[] widths1 = {95,105,95,125,125,110,125,90,60};//各列列宽设定
for(i=0;i<widths1.length;i++) {
tc = tcm.getColumn(i);
tc.setPreferredWidth(widths1[i]);
}
}
else if(tableName.equals("ServerLog")) {//服务器日志
int[] widths2 = {160,140,140,80};
for(i=0;i<widths2.length;i++) {
tc = tcm.getColumn(i);
tc.setPreferredWidth(widths2[i]);
}
}
else if(tableName.equals("TerminalLog")) {//终端日志
int[] widths3 = {75,80,75,140,140,65};
for(i=0;i<widths3.length;i++) {
tc = tcm.getColumn(i);
tc.setPreferredWidth(widths3[i]);
}
}
else if(tableName.equals("统计")) {//统计消息
//widths = {85,100,85,120,120,120,120,80};
}
else {
;;
//widths = null;
}
}
private TableColumnModel tcm;
private TableColumn tc;
private int i;
}
//end
import javax.swing.*;
import javax.swing.table.*;
/**
* 作者:alphazhao
* 日期:2002-5-25
* 描述:设定列表显示的各列列宽及其它设定,进入参数为tablename和JTable
* */
public class TableColumnSet {
public TableColumnSet(String tableName,JTable tableModel) {
tcm = tableModel.getColumnModel();
if(tableName.equals("UserLog")) {//用户
int[] widths1 = {95,105,95,125,125,110,125,90,60};//各列列宽设定
for(i=0;i<widths1.length;i++) {
tc = tcm.getColumn(i);
tc.setPreferredWidth(widths1[i]);
}
}
else if(tableName.equals("ServerLog")) {//服务器日志
int[] widths2 = {160,140,140,80};
for(i=0;i<widths2.length;i++) {
tc = tcm.getColumn(i);
tc.setPreferredWidth(widths2[i]);
}
}
else if(tableName.equals("TerminalLog")) {//终端日志
int[] widths3 = {75,80,75,140,140,65};
for(i=0;i<widths3.length;i++) {
tc = tcm.getColumn(i);
tc.setPreferredWidth(widths3[i]);
}
}
else if(tableName.equals("统计")) {//统计消息
//widths = {85,100,85,120,120,120,120,80};
}
else {
;;
//widths = null;
}
}
private TableColumnModel tcm;
private TableColumn tc;
private int i;
}
//end
解决方案 »
免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货