2012-07-13 31 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));