简体   繁体   English

Java Swing JTable列值和标头全角,怎么可能?

[英]Java Swing JTable column value and header full width, how is it possible?

Trying to make my JTable column width to be auto resized to it's value width. 试图使我的JTable列宽度自动调整为它的值宽度。

for example i have a column1 with text "just a test" and i get this result : 例如,我有一个column1,文本为“ just a test”,我得到以下结果:

|column1|
|-------|
|just ..|

but i need it to be like so: 但我需要像这样:

|column1    |
|-----------|
|just a test|

I've already searched whole JTable library for this one, can't find anything related. 我已经在整个JTable库中搜索了此库,找不到任何相关内容。 Any help? 有什么帮助吗?

First answer 第一个答案

To do this you should access the column of the table as this: 为此,您应该按以下方式访问表的列:

TableColumn column = null; column = table.getColumnModel().getColumn(/*here the index of your column*/); column.sizeWidthToFit();

be careful if header is empty or it will do nothing 标头为空时要小心,否则将无济于事

some of the sources just in case 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() 一些来源,以防万一https://docs.oracle.com/javase/tutorial/uiswing/components/table.html#width https://docs.oracle.com/javase/7/docs/api/javax/摆动/表/ TableColumn.html#sizeWidthToFit()

Update 更新

after some more search I found this previous post that should be a better answer to your problem Auto resizing the JTable column widths 经过更多搜索后,我发现该以前的帖子应该是您的问题的更好答案自动调整JTable列的宽度

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM