JTable如何设置列的宽度?

潍一 2012-12-24 10:15:48
我写一个学生管理系统,里面有列,但是自动默认都是同等长度的,有些列是身份证号码需要很长,但是有些是性别,只是一个字符而已,所以我不希望性别的列和身份证列的长度一样,太占空间了,求大神指教下,有什么方法设置宽度? JDK没查到,没办法!
...全文
274 4 打赏 收藏 转发到动态 举报
写回复
用AI写文章
4 条回复
切换为时间正序
请发表友善的回复…
发表回复
  • 打赏
  • 举报
回复
引用 3 楼 kiss8745 的回复:
引用 1 楼 nanman 的回复:Java code ? 12345678910111213141516171819202122232425262728293031 /** * 自动调整表列宽度 * * @param table 被调整表 * @param addtionalSpace 额外……
忽略
潍一 2012-12-24
  • 打赏
  • 举报
回复
引用 1 楼 nanman 的回复:
Java code ? 12345678910111213141516171819202122232425262728293031 /** * 自动调整表列宽度 * * @param table 被调整表 * @param addtionalSpace 额外的宽度 * @return 总列宽 */ ……
问题是他的返回值,我该怎么用呢?、
潍一 2012-12-24
  • 打赏
  • 举报
回复
//取得列的最大长度 private int getPreferredWidthForCloumn(JTable table,int icol){ TableColumnModel tcl = table.getColumnModel(); TableColumn col = tcl.getColumn(icol); int c = col.getModelIndex(),width = 0,maxw = 0; int[] iniCW = new int[table.getColumnCount()]; 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; } // 设定每列的宽度为当列的最大的宽度。 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); } } 找到这串代码,却爆Multiple markers at this line 这错误,不知道怎么用!
  • 打赏
  • 举报
回复
/**
     * 自动调整表列宽度
     *
     * @param table          被调整表
     * @param addtionalSpace 额外的宽度
     * @return 总列宽
     */
    public static int fitTableColumnsWidth(JTable table, int addtionalSpace) {
        table.setAutoResizeMode(JTable.AUTO_RESIZE_OFF);
        JTableHeader header = table.getTableHeader();
        int rowCount = table.getRowCount();

        Enumeration columns = table.getColumnModel().getColumns();
        int totalColumnWidth = 0;
        while (columns.hasMoreElements()) {
            TableColumn column = (TableColumn) columns.nextElement();
            int col = header.getColumnModel().getColumnIndex(column.getIdentifier());
            int width = (int) table.getTableHeader().getDefaultRenderer()
                    .getTableCellRendererComponent(table, column.getIdentifier()
                            , false, false, -1, col).getPreferredSize().getWidth();
            for (int row = 0; row < rowCount; row++) {
                int preferedWidth = (int) table.getCellRenderer(row, col).getTableCellRendererComponent(table,
                        table.getValueAt(row, col), false, false, row, col).getPreferredSize().getWidth();
                width = Math.max(width, preferedWidth);
            }
            header.setResizingColumn(column); // this line is very important
            column.setWidth(width + table.getIntercellSpacing().width + addtionalSpace);
            totalColumnWidth += width + table.getIntercellSpacing().width + addtionalSpace;
        }
        return totalColumnWidth;
    }

62,616

社区成员

发帖
与我相关
我的任务
社区描述
Java 2 Standard Edition
社区管理员
  • Java SE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧