Java JTable设置列宽 [英] Java JTable setting Column Width

查看:884
本文介绍了Java JTable设置列宽的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个JTable,我在其中设置列大小如下:

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?

我找到了 AUTO_RESIZE_LAST_COLUMN 属性在文档中,但它不起作用。

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

编辑: JTable 位于 JScrollPane 设置了首选大小。

JTable is in a JScrollPane its prefered size is set.

推荐答案

如果你打电话给 setMinWidth会怎样? 400)在最后一列 setPreferredWidth(400)

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

在JavaDoc for JTable 中,阅读 doLayout() 非常谨慎。以下是一些选择位:

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


当调整封闭窗口大小调整方法时,
resizingColumn一片空白。这意味着调整大小发生在JTable
的外部,并且更改 - 或delta - 应该分配给所有列,而不管
这个JTable的自动调整大小模式。

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.

这可能就是为什么 AUTO_RESIZE_LAST_COLUMN 对你没有帮助。

This might be why AUTO_RESIZE_LAST_COLUMN didn't help you.


注意:当JTable调整列的宽度时,它绝对尊重
的最小值和最大值。

Note: When a JTable makes adjustments to the widths of the columns it respects their minimum and maximum values absolutely.

这表示您可能希望为除最后一列之外的所有列设置Min == Max,然后在最后一列设置Min = Preferred列并且未设置Max或为Max。

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.

这篇关于Java JTable设置列宽的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆