Markysch
Markysch

Reputation: 47

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();

Upvotes: 1

Views: 1000

Answers (1)

ravibagul91
ravibagul91

Reputation: 20755

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);

Upvotes: 1

Related Questions