How can set getTreeCellRendererComponent in Java application

1k views Asked by At

Hi all I have a JXtreeTable with Custom DefaultTreeTableModel.

Now I want this:

  1. Change default icon with open folder icon (when the node is open), close folder icon (when the node is closed)
  2. I want change the all Row renderer of child node when it is open.

So I have this code:

public class CustomTreeTableSpeseXCategoriaSpese extends JLabel implements TreeCellRenderer, TableCellRenderer {
    /**
     * 
     */
    //DefaultTreeCellRenderer defaultRenderer = new 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 Font fontProva = new Font("Verdana", Font.BOLD, 20);


    @Override
    public Component getTableCellRendererComponent(JTable table, Object value, 
            boolean isSelected, boolean hasFocus, int row, int column) {
        setOpaque(true);



        if(column ==1){
            setHorizontalAlignment(SwingConstants.LEFT);
        }else{
            setHorizontalAlignment(SwingConstants.RIGHT);
        }

        if (row== table.getRowCount()-1) {
            setForeground(Color.BLACK);
            setFont(fontTotale);
            setBackground( Color.RED );
        }else if(row != table.getRowCount() && column !=4){
            setForeground( Color.BLACK );
            setBackground(new Color(200, 200, 200));
            setFont(UtilitySwing.getTableFont());
        }else if(row != table.getRowCount()-1 && column ==4){
            //verifico il valore se negativo rosso
            //se positivo blu
            String valore = value.toString();
            if(valore.startsWith("-")){
                setForeground(Color.red);
                setFont(fontNegativo);
            }else{
                setForeground(Color.blue);
                setFont(fontNegativo);
            }
        }
        setText(value != null ? value.toString() : "<null>");
        return this;
    }

SEE EDIT COMMENT
public Component getTreeCellRendererComponent(JTree tree, Object value,
            boolean selected, boolean expanded, boolean leaf, int row,
            boolean hasFocus) {}
        
    } 

With this code, I don't have a correct layout. I see this if I try to run my application enter image description here

I can not understand why I see over the icon also the string of Object like com.mcsolution.beans.Entrata@11111

EDIT I change the method getTreeCellendereComponent with this:

public Component getTreeCellRendererComponent(JTree tree, Object value,
            boolean selected, boolean expanded, boolean leaf, int row,
            boolean hasFocus) {
        setOpaque(true);

        RandomTextTreeTableModel model = (RandomTextTreeTableModel) tree.getModel();
        
        
        if (model.getPathToRoot((TreeTableNode) value).length > 2) {
            //Do your rendering goodness here
            //setForeground( Color.BLACK );
            setBackground(Color.white);
            
        }
        
        if (expanded) {
            ImageIcon rendererIcon = new ImageIcon(getClass().getResource("/resources/folder_open.png"));
            setIcon(rendererIcon);
            //setBackground(Color.green);
            //setFont(fontProva);
        }else{
            ImageIcon rendererIcon = new ImageIcon(getClass().getResource("/resources/folder_close.png"));
            setIcon(rendererIcon);
        }
        
        //se non ha figli
        if(leaf){
            setIcon(null);
        }
        setText(value != null ? value.toString() : "<null>");
        return this;
    }

Now I can see correct the Icon but I continue to see the write com.mcsolution..... and I can't set another font with child node.

0

There are 0 answers