2012-07-13 53 views
2

我试图使用SwingX的TableColumnExt类来为JXTreeTable的层级列中的列宽度设置原型值。我初始化模型和表格后,我这样做:TableColumnExt不尊重原型值

TableColumnExt column = dataTable.getColumnExt(0);    
column.setPrototypeValue(500); 

当我的表格呈现时,所有列的大小相同。这些是我在JXTreeTable实例上使用的方法。

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

我在做什么错在这里?

+0

如果您希望列的宽度为500像素,这是一个误解:prototypeValue是作为实际值f.i的替身。尝试setPrototypeValue(“我真的很长,非常重要的列内容越来越长”)。也就是说,它可能是因为它在层次结构列上不能正常工作 - 如果是这样,你再次遇到错误,并可能考虑在SwingX问题跟踪器中报告问题:-),谢谢 – kleopatra 2012-07-14 09:20:14

+0

验证它是一个错误, http://java.net/jira/browse/SWINGX-1509 - 感谢您将它引入我们的注意 – kleopatra 2012-07-16 10:27:10

+0

很高兴我能帮上忙!有关解决方法的任何想法? – whatknight 2012-07-16 13:04:03

回答

2

正如我在评论中提到的it's a bug。这些问题是多方面的:

  • 负责测量原型尺寸要求的代码是ColumnFactory.calcPrototypeWidth
  • 它是由配置渲染为与给定的原型返回该表并测量其prefSize
  • 这样做
  • 的分层列的渲染委托给一个JTree,使用黑魔法
  • 出厂默认是不知道的黑魔法,因此查询的JTree的prefSize独立于原型
  • 第一想法:使工厂意识到分层列的特殊渲染,并走到真正的底层treeCellRenderer - 不起作用,再次由于其他一些内部的黑魔法
  • 自动列重新调整大小由于设置原型被打破JXTable(Issue #1510

一种解决方法涉及

  • 定制ColumnFactory是知道的特殊渲染分层柱(== JXTree))
  • 为分层列的,配置和措施一个TreeCellRenderer的 - 而不是一个由树本身使用,但列

下面的不相关的虚拟

  • 设置原型后,手动触发大小的评价是一个自定义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));