Auto resize the widths of JTable's columns dynamically

前端 未结 3 460
隐瞒了意图╮
隐瞒了意图╮ 2020-12-11 03:07

I have a JTable with 3 columns:

- No. #
- Name
- PhoneNumber

I want to make specific width for each column as follows:

3条回答
  •  醉酒成梦
    2020-12-11 04:00

    I actually run into this problem too. I've found one useful link that solved my issue. Pretty much get the specific column and set its setMinWidth and setMaxWidth to be the same(as fixed.)

    private void fixWidth(final JTable table, final int columnIndex, final int width) {
        TableColumn column = table.getColumnModel().getColumn(columnIndex);
        column.setMinWidth(width);
        column.setMaxWidth(width);
        column.setPreferredWidth(width);
    }
    

    Ref: https://forums.oracle.com/thread/1353172

提交回复
热议问题