java jtable设置列宽,Java JTable设置列宽

当JTable设置为不自动调整列宽后,最大化窗口时最后一列右侧出现空隙。通过分析JTable的doLayout()方法,发现需要设置最小宽度而不是首选宽度来避免这个问题。对于最后一列,设置最小宽度为400,其他列的最小和最大宽度相等,以确保在窗口调整时,最后一列能填充到窗口的末端。

I have a JTable in which I set the column size as follows:

table.setAutoResizeMode(JTable.AUTO_RESIZE_OFF);

table.getColumnModel().getColumn(0).setPreferredWidth(27);

table.getColumnModel().getColumn(1).setPreferredWidth(120);

table.getColumnModel().getColumn(2).setPreferredWidth(100);

table.getColumnModel().getColumn(3).setPreferredWidth(90);

table.getColumnModel().getColumn(4).setPreferredWidth(90);

table.getColumnModel().getColumn(6).setPreferredWidth(120);

table.getColumnModel().getColumn(7).setPreferredWidth(100);

table.getColumnModel().getColumn(8).setPreferredWidth(95);

table.getColumnModel().getColumn(9).setPreferredWidth(40);

table.getColumnModel().getColumn(10).setPreferredWidth(400);

This works fine, but when the table is maximized, I get empty space to the right of the last column. Is it possible to make the last column resize to the end of the window when resized?

I found AUTO_RESIZE_LAST_COLUMN property in docs but it does not work.

Edit: JTable is in a JScrollPane its prefered size is set.

解决方案

What happens if you call setMinWidth(400) on the last column instead of setPreferredWidth(400)?

In the JavaDoc for JTable, read the docs for doLayout() very carefully. Here are some choice bits:

When the method is called as a result of the resizing of an enclosing window, the

resizingColumn is null. This means that resizing has taken place "outside" the JTable

and the change - or "delta" - should be distributed to all of the columns regardless of

this JTable's automatic resize mode.

This might be why AUTO_RESIZE_LAST_COLUMN didn't help you.

Note: When a JTable makes adjustments to the widths of the columns it respects their

minimum and maximum values absolutely.

This says that you might want to set Min == Max for all but the last columns, then set Min = Preferred on the last column and either not set Max or set a very large value for Max.

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值