Domanda

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();
È stato utile?

Soluzione

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);
Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top