0

这是我的单元格渲染器类

    public class MyCellRenderer extends DefaultTreeCellRenderer{

    ImageIcon parentbill = new ImageIcon("images/parentbill.png");

    ImageIcon childbill = new ImageIcon("images/childbill.png");

    ImageIcon parentPurchase = new ImageIcon("images/parentexpense.png");

    ImageIcon childPurchase = new ImageIcon("images/childexpense.png");

    public MyCellRenderer(TreeTableController contr){
        super();
    }
    public Component getTreeCellRendererComponent(JTree tree, Object value, boolean selected, boolean expanded, boolean leaf, int row, boolean hasFocus) {
            Component c = super.getTreeCellRendererComponent(tree, value, selected, expanded, leaf, row, hasFocus);
           if(leaf){

                setIcon(childPurchase);
                //((DefaultTreeCellRenderer) c).setClosedIcon(parentPurchase);
            }else{
                setIcon(parentPurchase);    
            }

            if(expanded){
                setIcon(parentPurchase);
            }

            return c;
        }
}

还有我如何添加它:

    TableModel tableModel = new TableModel(root);
    JXTreeTable treeTable = new JXTreeTable(tableModel);
    treeController = new TreeTableController(tableModel, treeTable);

    MyCellRenderer cellRenderer = new MyCellRenderer(treeController);
    treeController.getTable().setTreeCellRenderer(cellRenderer);

因此,我的渲染器应该为表中不同类型的行更改图标。而且,他做到了(哇!)。但是他首先弄乱了 table 应该如何看待,他在第一个单元格中放了一些 java 垃圾,如下所示: 我该它假设在第一个单元格中有购买字,但只有当我点击它时才会出现 如何修复它?我知道,有一种方法可以在没有渲染器的情况下从 JXTreeTable 设置图标,但我想根据行的类型添加一些条件,所以我必须覆盖它。

4

0 回答 0