TableColumn MinWidth 不变
我有一个可动态地将一列的宽度更改为设置值的JTable,并根据用户输入再次返回。但是,我无法更改列的最小,宽度和最大宽度属性。
// Declared within the class so it persists during run time
TableColumn firstColumn;
在初始化的方法中,设置了显示
JTable cellTable = new JTable(cellDataModel);
cellTable.setRowSorter(myTableRowSorter);
cellTable.setDefaultRenderer(Object.class, cellRender);
cellTable.setAutoResizeMode(JTable.AUTO_RESIZE_OFF); // Tried both off and on, no difference
TableColumnModel tcm = cellTable.getColumnModel();
tcm.getColumn(0).setPreferredWidth(120);
tcm.getColumn(0).setMinWidth(120); // This correctly sets the column's width to 120.
cellTable.setFillsViewportHeight(true);
firstColumn = tcm.getColumn(0);
CHKHIDE的控制台是一个用于调整第一列大小的JCHECKBOX。
private void chckHideClicked(ActionEvent ae) {
int width = chkHide.isSelected() ? 80:120;
System.out.println("Set width to " + Integer.toString(width));
firstColumn.setMinWidth(width);
firstColumn.setPreferredWidth(width);
firstColumn.setMaxWidth(width);
System.out.println("My width is now " + Integer.toString(firstColumn.getWidth()) +
" and minWidth is now " + Integer.toString(firstColumn.getMinWidth()));
updateData(); // Forces an update of the table model and table
}
当我运行代码时,第一次检查复选框将第一列的宽度从120降低到预期的80。但是,随后的复选框点击没有效果。使用调试器,我已经确认setMinwidth(120)对最小宽没有影响。控制台输出是:
Set width to 80
My width is now 80 and minWidth is now 80
Set width to 120
My width is now 80 and minWidth is now 80
Set width to 80
My width is now 80 and minWidth is now 80
Set width to 120
My width is now 80 and minWidth is now 80
表模型和单元格渲染器都没有任何明确更改列宽度的代码。列宽度为什么不更新宽度值?
I have a JTable where I want to dynamically change the width of one column to a set value and back again based on user input. However, I cannot change the column's MinWidth, Width and MaxWidth properties.
// Declared within the class so it persists during run time
TableColumn firstColumn;
Within the Initialize method that setups up the console for display
JTable cellTable = new JTable(cellDataModel);
cellTable.setRowSorter(myTableRowSorter);
cellTable.setDefaultRenderer(Object.class, cellRender);
cellTable.setAutoResizeMode(JTable.AUTO_RESIZE_OFF); // Tried both off and on, no difference
TableColumnModel tcm = cellTable.getColumnModel();
tcm.getColumn(0).setPreferredWidth(120);
tcm.getColumn(0).setMinWidth(120); // This correctly sets the column's width to 120.
cellTable.setFillsViewportHeight(true);
firstColumn = tcm.getColumn(0);
chkHide is a JCheckBox used to resize the first column.
private void chckHideClicked(ActionEvent ae) {
int width = chkHide.isSelected() ? 80:120;
System.out.println("Set width to " + Integer.toString(width));
firstColumn.setMinWidth(width);
firstColumn.setPreferredWidth(width);
firstColumn.setMaxWidth(width);
System.out.println("My width is now " + Integer.toString(firstColumn.getWidth()) +
" and minWidth is now " + Integer.toString(firstColumn.getMinWidth()));
updateData(); // Forces an update of the table model and table
}
When I run the code, the first check of the checkbox reduces the width of the first column from 120 to 80 as expected. However, subsequent clicks of the checkbox have no effect. Using the debugger, I've confirmed that setMinWidth(120) has no effect on MinWidth. The console output is:
Set width to 80
My width is now 80 and minWidth is now 80
Set width to 120
My width is now 80 and minWidth is now 80
Set width to 80
My width is now 80 and minWidth is now 80
Set width to 120
My width is now 80 and minWidth is now 80
Neither the table model nor the cell renderer have any code that explicitly changes the column width. Why won't the column width update width values?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论