0

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.

Community
  • 1
  • 1
bircastri
  • 2,169
  • 13
  • 50
  • 119
  • Try the examples [here](http://stackoverflow.com/a/14097270/230513) and [here](http://stackoverflow.com/a/4641530/230513). – trashgod Nov 26 '15 at 17:01
  • Ok, with your example, I can set correct closed or open image, but for the other my question? – bircastri Nov 27 '15 at 08:29
  • What's not working? Please edit your question to include a [mcve] that focuses on a single question and shows your current approach. – trashgod Nov 27 '15 at 12:02
  • The "com.mcsolution.beans.Entrata@11111" looks like the default `toString` implementation: "[class name]@[hash code]". If you add more code to your question to make it into a complete example, we will probably be able to help you further. At the moment, people have to guess how the tree table is constructed, which makes it very difficult to be sure what the problem is and how to fix it. – Freek de Bruijn Nov 27 '15 at 21:34

0 Answers0