Java Swing JTable 列值和 header 全宽,这怎么可能?
Java Swing JTable column value and header full width, how is it possible?
正在尝试使我的 JTable 列宽自动调整为其值宽度。
例如,我有一个带有文本 "just a test" 的 column1,我得到了这个结果:
|column1|
|-------|
|just ..|
但我需要它像这样:
|column1 |
|-----------|
|just a test|
我已经在整个 JTable 库中搜索了这个,找不到任何相关内容。有帮助吗?
第一个回答
为此,您应该按如下方式访问 table 的列:
TableColumn column = null;
column = table.getColumnModel().getColumn(/*here the index of your column*/);
column.sizeWidthToFit();
当 header 为空时要小心,否则它什么都不做
一些来源以防万一
https://docs.oracle.com/javase/tutorial/uiswing/components/table.html#width
https://docs.oracle.com/javase/7/docs/api/javax/swing/table/TableColumn.html#sizeWidthToFit()
更新
经过更多搜索后,我发现之前的 post 应该可以更好地解决您的问题
Auto resizing the JTable column widths
正在尝试使我的 JTable 列宽自动调整为其值宽度。
例如,我有一个带有文本 "just a test" 的 column1,我得到了这个结果:
|column1|
|-------|
|just ..|
但我需要它像这样:
|column1 |
|-----------|
|just a test|
我已经在整个 JTable 库中搜索了这个,找不到任何相关内容。有帮助吗?
第一个回答
为此,您应该按如下方式访问 table 的列:
TableColumn column = null;
column = table.getColumnModel().getColumn(/*here the index of your column*/);
column.sizeWidthToFit();
当 header 为空时要小心,否则它什么都不做
一些来源以防万一 https://docs.oracle.com/javase/tutorial/uiswing/components/table.html#width https://docs.oracle.com/javase/7/docs/api/javax/swing/table/TableColumn.html#sizeWidthToFit()
更新
经过更多搜索后,我发现之前的 post 应该可以更好地解决您的问题 Auto resizing the JTable column widths