Set a column width of JTable according to the text in a header

前端 未结 3 1166
感动是毒
感动是毒 2020-12-19 22:51

I need to get an appropriate table size according to a text in a header. It contains abbreviations of czech names of days like: \"Po\", \"Út\", \"St\", atc. but instead of t

3条回答
  •  情话喂你
    2020-12-19 23:30

    I came to solution :-)

    I had to change the look and feel before I calculated appropriate column widths. When I did it the other way around, the calculation was out of date.

    So I only needed to set the correct widths like this:

    table.getColumnModel().getColumn(col).setPreferredWidth(width);
    

    And then change the size of the viewPort like this, because the table's prefered size is now correct.

    table.setPreferredScrollableViewportSize(table.getPreferredSize());
    

    Thanks! ;)

提交回复
热议问题