javaswingswingxjtreetabletreecellrenderer

How to set custom DefaultTreeCellRenderer at JXTreeTable


I want to set my custom DefaultTreeCellRenderer.

So I have built this 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; 
    }
} 

With this code I can't see my background color, my foreground color. 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.

EDIT

With that code, I can see the JXTreeTable like this:

enter image description here

But I want show the TreeTable like this:

enter image description here

EDIT

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; 
    }
} 

Solution

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

    It's hard to tell because you don't show how do you set the renderer to your tree table, which leads to your second question.

    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 is composed by two parts a tree and a table and thus you have to set both a TreeCellRenderer and a TableCellRenderer to make custom rendering. Something like this:

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

    You can make a single class that implements both interfaces as shown in this Q/A