public class MyTableHeaderUI extends javax.swing.plaf.basic.BasicTableHeaderUI {
    
    protected void installListeners() {
        super.installListeners();
        
        MouseInputHandler mouseInputListener = new MouseInputHandler();
        header.addMouseListener(mouseInputListener);
    }
    
    public class MouseInputHandler extends MouseInputAdapter {        public void mouseClicked(MouseEvent e) {
            Point p = e.getPoint();
            int clickCount = e.getClickCount();            JTable thisTable  = header.getTable();
            FontMetrics fm = thisTable.getFontMetrics(thisTable.getFont());
            TableColumnModel columnModel = header.getColumnModel();
            int index = getResizingColumn(p, columnModel.getColumnIndexAtX(p.x));
            int rows = thisTable.getRowCount();
            int maxLength = 0;
            
            if (clickCount > 1) {
                if (index != -1 && rows > 0) {
                    if (canResize(index)) {
                        for (int i=0; i<rows; i++) {
                            Object objValue = thisTable.getValueAt(i, index);
                            if (objValue instanceof java.lang.String) {
                                int thisLen = fm.stringWidth((String) objValue);
                                maxLength = Math.max(maxLength, thisLen);
                            }
                        }
                        if (maxLength > 0)
                            thisTable.getColumnModel().getColumn(index).setPreferredWidth(maxLength + 7);
                    }
                }
            }
        }     private boolean canResize(int index) {
        TableColumn tblColumn = header.getColumnModel().getColumn(index);
        return (tblColumn != null) && header.getResizingAllowed() && tblColumn.getResizable();
    }        private int getResizingColumn(Point p, int column) {
            if (column == -1) {
                return -1;
            }
        Rectangle r = header.getHeaderRect(column);
        r.grow(-3, 0);
        if (r.contains(p)) {
        return -1;
        }
        int midPoint = r.x + r.width/2;
        int columnIndex = (p.x < midPoint) ? column - 1 : column;
        
        return columnIndex; 
        }    }
}实现了类似与windows下面双击表头自动伸缩的功能。 希望对你有点参考。

解决方案 »

  1.   

    我在项目中也做过这个功能。给你两个现成的函数吧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);
        }
      

  2.   

    谢谢楼上
    请问iniCW[i]是什么?一定给分!
      

  3.   

    对不起,我刚看贴 
    iniCW[i]是我自己设定得一个初始的长度,有的时候列太窄了难看。你也可以自己定义。没有什麽影响