如何在 JXTreeTable 中设置自定义 DefaultTreeCellRenderer

How to set custom DefaultTreeCellRenderer at JXTreeTable

我想设置我的自定义 DefaultTreeCellRenderer。

所以我构建了这个 class:

class CustomTreeTableSpeseXCategoriaSpese extends DefaultTreeCellRenderer {
    /**
     * 
     */
    private static final long serialVersionUID = 4842418316518803090L;
    private Font fontTotale = new Font("Verdana", Font.BOLD, 12);
    private Font fontNegativo = new Font("Verdana", Font.BOLD, 12);
    private int numeroRighe;
    CustomTreeTableSpeseXCategoriaSpese(int numeroRighe){
        this.numeroRighe=numeroRighe;
    }

    public Component getTreeCellRendererComponent(
            JTree tree,
            Object value,
            boolean selected,
            boolean expanded,
            boolean leaf,
            int row,
            boolean hasFocus) {
        // Allow the original renderer to set up the label
        Component rendererComponent = super.getTreeCellRendererComponent(
                tree, value, selected,
                expanded, leaf, row,
                hasFocus); 



        rendererComponent.setBackground( Color.WHITE );
        if (row== this.numeroRighe-1) {
            rendererComponent.setForeground(Color.BLACK);
            rendererComponent.setBackground( Color.RED );
            rendererComponent.setFont(fontTotale);
        }else if(row != this.numeroRighe/* && column !=3*/){
            rendererComponent.setForeground( Color.BLACK );
            rendererComponent.setBackground(new Color(200, 200, 200));
        }else if(row != this.numeroRighe-1 /*&& column ==3*/){


        }


        return rendererComponent; 
    }
} 

使用此代码我看不到我的背景色和前景色。 另一个问题是,是否可以为不同的列设置不同的渲染器?我希望前 3 列有背景色和前景色,因为另一列有不同的渲染器。

编辑

使用该代码,我可以看到这样的 JXTreeTable:

但我想像这样显示 TreeTable:

编辑

public class CustomTreeTableSpeseXCategoriaSpese extends JLabel implements TreeCellRenderer, TableCellRenderer {
    /**
     * 
     */
    private static final long serialVersionUID = 4842418316518803090L;
    private Font fontTotale = new Font("Verdana", Font.BOLD, 12);
    private Font fontNegativo = new Font("Verdana", Font.BOLD, 12);

    @Override
    public Component getTableCellRendererComponent(JTable table, Object value, boolean isSelected, boolean hasFocus, int row, int column) {
        setOpaque(true);
        setBackground(isSelected ? new Color(83,142,213) : Color.white);
        setForeground(isSelected ? Color.white : Color.black);
        setText(value != null ? value.toString() : "<null>");
        return this;
    }

    public Component getTreeCellRendererComponent(JTree tree, Object value, boolean selected, boolean expanded, boolean leaf, int row, boolean hasFocus) {
        //rendererComponent.setBackground( Color.WHITE );
        if (row== tree.getRowCount()) {
            setForeground(Color.BLACK);
            setOpaque(true);
            setBackground( Color.RED );
            setFont(fontTotale);
        }else if(row != tree.getRowCount()/* && column !=3*/){
            setForeground( Color.BLACK );
            setOpaque(true);
            setBackground(new Color(200, 200, 200));
        }else if(row != tree.getRowCount()-1 /*&& column ==3*/){
            //verifico il valore se negativo rosso
            //se positivo blu
            /*String valore = super.getValueAt(row, column).toString();
            if(valore.startsWith("-")){
                rendererComponent.setForeground(Color.red);
                rendererComponent.setFont(fontNegativo);
            }else{
                rendererComponent.setForeground(Color.blue);
                rendererComponent.setFont(fontNegativo);
            }*/
        }
         setText(value != null ? value.toString() : "<null>");
        return this; 
    }
} 

With this code I can't see my background color, my foreground color.

很难说,因为您没有说明如何将渲染器设置到您的树 table,这导致了您的第二个问题。

Another question is, it is possibile to set different Renderer for different column? I want the first 3 column have a background and foreground color, for the other column have a different renderer.

A JXTreeTable 由树和 table 两部分组成,因此您必须同时设置 TreeCellRendererTableCellRenderer 才能进行自定义渲染。像这样:

treeTable.setDefaultRenderer(Object.class, new CustomTableCellRenderer());
treeTable.setTreeCellRenderer(new CustomTreeCellRenderer());

您可以创建一个实现两个接口的 class,如 this Q/A

所示