简体   繁体   中英

How to resize a single JTable column without affecting the other table columns

I'm trying to resize a JTable column without affecting the rest of the columns in the table but every time I try to do this all the other columns also change size:

    final JTableHeader header = (JTableHeader) e.getSource();
    header.getTable().setAutoResizeMode(JTable.AUTO_RESIZE_OFF);
    TableColumn column = header.getColumnModel().getColumn(3);                              
    for (int col = 0; col < header.getColumnModel().getColumnCount() ; col++) {
        if (col == column.getIndex()) {
            header.getColumnModel().getColumn(col).setPreferredWidth(header.getColumnModel().getColumn(col).getPreferredWidth()+100);
            break;
        }                    
    }
    columnModel.getTable().doLayout();

If you want to change only one column's size then no need to do such long procedure. You can do it in a single line like:

table.getColumnModel().getColumn(3).setPreferredWidth(table.getColumnModel().getColumn(3).getPreferredWidth()+100);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM