我正在尝试使用SwingX的TableColumnExt
类为JXTreeTable
的分层列中的列宽设置原型值。在我初始化模型和表格后,我这样做:
TableColumnExt column = dataTable.getColumnExt(0);
column.setPrototypeValue(500);
渲染我的表时,所有列的大小都相同。这些是我在JXTreeTable
实例上使用的方法。
dataTable.setRowHeight(28);
dataTable.setFillsViewportHeight(true);
dataTable.setHorizontalScrollEnabled(true);
我在这里做错了什么?
答案 0 :(得分:2)
正如我在评论中已经提到的,it's a bug。问题很多:
解决方法涉及
下面是一个自定义的ColumnFactory及其用法(未经过正式测试,所以请耐心等待:-)。
// a custom factory
ColumnFactory factory = new ColumnFactory() {
@Override
protected int calcPrototypeWidth(JXTable table,
TableColumnExt columnExt) {
if (isHierarchicalPrototype(table, columnExt)) {
return calcHierarchicalPrototypeWidth((JXTreeTable) table, columnExt);
}
return super.calcPrototypeWidth(table, columnExt);
}
protected boolean isHierarchicalPrototype(JXTable table,
TableColumnExt columnExt) {
return (table instanceof JXTreeTable)
&& ((JXTreeTable) table).getTreeTableModel().getHierarchicalColumn() ==
columnExt.getModelIndex()
&& columnExt.getPrototypeValue() != null;
}
TreeCellRenderer dummy = new DefaultTreeCellRenderer();
protected int calcHierarchicalPrototypeWidth(JXTreeTable table,
TableColumnExt columnExt) {
JXTree renderer = (JXTree) getCellRenderer(table, columnExt);
// commented lines would be the obvious step down into the "real" sizing
// requirements, but giving reasonable result due to internal black magic
// TreeCellRenderer treeRenderer = renderer.getCellRenderer();
// Component comp = treeRenderer.getTreeCellRendererComponent(renderer,
columnExt.getPrototypeValue(), false, false, false, -1, false);
// instead, measure a dummy
Component comp = dummy.getTreeCellRendererComponent(renderer,
columnExt.getPrototypeValue(), false, false, false, -1, false);
return Math.max(renderer.getPreferredSize().width, comp.getPreferredSize().width);
}
};
// usage: first create the treeTable, set the factory and set the model
JXTreeTable table = new JXTreeTable();
table.setColumnFactory(factory);
table.setTreeTableModel(new FileSystemModel());
// set the prototype
table.getColumnExt(0).setPrototypeValue("long longer longest still not enough to really see" +
" some effect of the prototype if available");
// Issue #1510: prototype value handling broken in underlying JXTable
// need to manually force the config
table.getColumnFactory().configureColumnWidths(table, table.getColumnExt(0));