Java TableColumnText不考虑原型值

Java TableColumnText不考虑原型值,java,swing,swingx,jxtreetable,Java,Swing,Swingx,Jxtreetable,我试图使用SwingX的tableColumnText类在JXTreeTable的层次结构列中设置列宽的原型值。初始化模型和表后,我执行以下操作: TableColumnExt column = dataTable.getColumnExt(0); column.setPrototypeValue(500); 呈现表格时,所有列的大小都相同。这些是我在JXTreeTable实例上使用的方法 dataTable.setRowHeight(28); dataTable.se

我试图使用SwingX的
tableColumnText
类在
JXTreeTable
的层次结构列中设置列宽的原型值。初始化模型和表后,我执行以下操作:

TableColumnExt column = dataTable.getColumnExt(0);            
column.setPrototypeValue(500);
呈现表格时,所有列的大小都相同。这些是我在
JXTreeTable
实例上使用的方法

dataTable.setRowHeight(28);
dataTable.setFillsViewportHeight(true);
dataTable.setHorizontalScrollEnabled(true);

我在这里做错了什么?

正如我在评论中已经提到的。问题有很多方面:

  • 负责测量原型尺寸要求的代码是ColumnFactory.calcPrototypeWidth
  • 它是通过使用给定的原型配置表返回的渲染器并测量其大小来实现的
  • 使用黑魔法将层次列的呈现委托给JTree
  • 默认工厂不知道这一黑魔法,因此查询JTree的prefSize,它独立于原型
  • 第一个想法:让工厂意识到层次列的特殊渲染,然后走到真正的底层treeCellRenderer,同样由于其他一些内部黑魔法,它不起作用
  • 由于在JXTable()中设置原型而自动调整列大小
解决方法包括

  • 了解分层列的特殊呈现程序(=JXTree)的自定义ColumnFactory)
  • 对于Hierarchy列,配置并度量一个TreeCellRenderer—不是树本身使用的那个,而是一个不相关的伪对象
  • 设置原型后,手动触发柱的尺寸评估
下面是一个定制的ColumnFactory及其用法(未经过正式测试,因此请恕我直言:-)


如果您希望该列的宽度为500 px,那么这是一个误解:prototypeValue意味着作为实际值的替代,f.i.尝试setPrototypeValue(“我的非常非常非常长而且非常重要的列内容,它变得越来越长”)。这就是说,这可能是它不能正常工作的层次列-如果你再次击中一个bug,可能会考虑报告一个问题在SWIXX问题跟踪器:-)感谢验证它是一个错误,归档-谢谢你把它带给我们的关注,我可以帮助!有什么解决办法吗?
// 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));