如何在 JTable 中排除自动调整大小的列
我有一个 JTable
,其中在 TableColumnModel 中指定了 5 列。我希望表格使用面板中所有可用的水平空间(可能会有所不同),但我想将调整大小限制为最后三行。
更详细地说:
前两列使用首选宽度进行初始化,但是用户应该能够通过拖动鼠标来手动调整列的大小。
最后三列应平均分配前两列未使用的所有剩余空间。编辑:它们还需要手动调整大小。
我尝试过以下方法: 将表设置为 autoResizeMode JTable.AUTO_RESIZE_ALL_COLUMNS
。这成功了 用尽了所有可用空间,但(显然)它还调整了前两列的大小。 我对此的反应是为前两列设置最小值和最大值。那行得通, 但这违反了我手动调整它们大小的限制。
JTable
-API 中是否有一种简单的方法可以实现此目的?
提前致谢!
I have a JTable
with 5 Columns specified in a TableColumnModel. I want the table to use all available horizontal space in the panel (which can vary), but I want to limit the resizing to the last three rows.
In more detail:
The first two columns are initialized with a preferred width, however the user should be able to resize the column manually by dragging with the mouse.
The last three columns should divide equally all the remaining space that isn't being used by the first two columns. EDIT: They also need to be resizable manually.
I've tried the following:
Setting the table to autoResizeMode JTable.AUTO_RESIZE_ALL_COLUMNS
. This suceeded in
using up all available space, but (obviously) it also resized the first two columns.
I've reacted to this by setting a min and max for the first two columns. That worked,
but it violated my constraint of having them resizable manually.
Is there a simple way in the JTable
-API to achieve this?
Thanks in advance!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
正如 Thomas 已经回答的那样,没有简单的 api 可以实现您想要的功能:您必须自己实现该行为。这在一般情况下可能不是微不足道的,但在您的具体情况下是可以管理的。只是为了确保我正确理解您的要求:
A)用户可以调整 2 列的大小,用户不能调整 3 列的大小(它们应该具有相同的宽度)
B)当用户调整 2 个可调整大小的列之一的大小时,其他可调整大小的列应保持其当前宽度,任何空间分布应仅影响 3 个不可调整大小的列,将多余/缺失的宽度均匀分布在它们之间。
基本上,技巧是在调整其中一列的大小时“修复”其他可调整大小的列的大小。 “修复”是通过将其最小值/最大值设置为其首选来实现的。然后,在 AUTO_RESIZE_ALL_COLUMNS 模式下,剩余列的空间均匀分布将自动发生。问题是检测调整大小开始/停止: JTableHeader 有一个属性 resizingColumn ,不幸的是,该属性在核心中未绑定(又名:不会在更改时触发)。另一方面,SwingX(您已经猜到我迟早会提到 SwingX :-)将该属性提升为成熟的 bean 属性 - 因此只需侦听并根据需要更新最小/最大大小约束即可。这是使用 SwingX 的代码片段
这是为了懒惰而设计的 SwingX - 很容易自己实现:
a) 手动配置 tableColumns
b) 子类化 JTableHeader 并实现 setResizingColumn 以在更改时触发
c) 子类化 JTable 并实现 createTableHeader 以返回子类化的标头
享受吧!
珍妮特
As Thomas already answered, there is no simple api to achieve what you want: you have to implement the behaviour yourself. That might not be trivial in a general context, but manageable in your concrete context. Just to ensure I do understand your requirement correctly:
A) 2 columns are resizable by the user, 3 columns are not resizable by the user (they should have the same width)
B) while the user resizes one of the 2 resizable columns, the other resizable columns should keep its current width, any space distribution should effect the 3 not-resizable columns only, evenly distributing the excess/missing width across them.
Basically, the trick is to "fix" the size of the other resizable columns while resizing one of them. The "fix" is achieved by setting both its min/max to its preferred. Then the even distribution of space across the remaining columns will happen automatically in AUTO_RESIZE_ALL_COLUMNS mode. The problem is to detect the resizing start/stop: JTableHeader has a property resizingColumn which unfortunately is unbound (aka: doesn't fire on change) in core. SwingX, on the other hand, (you already guessed I would come to mention SwingX sooner or later :-) promoted that property to a full-fledged bean property - so simply listen and update the min/max size constraints as appropriate. Here's a code snippet using SwingX
This is SwingX for lazyness - easy enough to implement yourself:
a) configure the tableColumns manually
b) subclass JTableHeader and implement setResizingColumn to fire on change
c) subclass JTable and implement createTableHeader to return the subclassed header
Enjoy!
Jeanette
AFAIK 没有办法将列排除在自动调整大小之外。设置最小/最大大小会影响大小计算,但从 JTable 源来看,您似乎无法排除列。
当使用
JTable.AUTO_RESIZE_ALL_COLUMNS
时,无法限制要调整大小的列的范围。因此,调用表模型来设置每列的列宽并应用约束。但是,我看不出表格模型有任何方式可以区分自动和手动调整大小。AFAIK there's no way to exclude the columns from autoresizing. Setting a min/max size affects the size calculation but from the JTable source it seems that you can't exclude columns.
When
JTable.AUTO_RESIZE_ALL_COLUMNS
is used, the range of columns to be resized can't be restricted. Thus, the table model is called to set the column width for each column and there constraints are applied. However, I don't see any way the table model can distinguish between automatic and manual resize.