0

I have a JXtreeTable in my java swing application. I want to set a customize cellrenderer, so I want to have a custom font for the row parent and I want to set another custom font for the child node (lead node), but I'm not able to du this.

This is my code:

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

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

    @Override
    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) {
            setBackground(Color.white);
        }

        if (expanded) {
            ImageIcon rendererIcon = new ImageIcon(getClass().getResource("/resources/folder_open.png"));
            setIcon(rendererIcon);
        }else{
            ImageIcon rendererIcon = new ImageIcon(getClass().getResource("/resources/folder_close.png"));
            setIcon(rendererIcon);
        }

        //se non ha figli
        if(leaf){
            setIcon(null);
            this.setBackground(Color.red);      
        }else{
            this.setBackground(Color.white);
        }
        setText(value != null ? "" : "<null>");
        return this;
    }
} 

If I try to run this code, I can see this:

enter image description here

The first row have the correct Font, and now I want to change the font of the child row.

mKorbel
  • 109,525
  • 20
  • 134
  • 319
bircastri
  • 2,169
  • 13
  • 50
  • 119
  • there is TableCellRenderer, search for SwingX Rendereres, – mKorbel Nov 27 '15 at 09:33
  • I have search also this but this not fixed my problems – bircastri Nov 27 '15 at 09:39
  • If I understand you, your problem is to identify within you renderer that a particular call is made for a parent row or a child row. I would go 3 ways. 1) based on the value : Are the values for parent rows and child rows of different classes ? If yes, you could identify the rows with "instanceof". 2) go back to the model : based on the row and some model.getValueAt(row) you may know if you are dealing with a header or a child. 3) The value in the "getTableCellRendererComponent" method should be a AbstractMutableTreeTableNode. This class has methods such as "children()", "isLeaf()", ... – lvr123 Nov 27 '15 at 10:02
  • Did the suggestions made by [lvr123](http://stackoverflow.com/users/2398993/lvr123) solve your problem? Are you trying to change the font in the tree part and/or the table part? Is your question how to determine whether in your `TableCellRenderer` a table cell is related to a parent or child node? – Freek de Bruijn Nov 29 '15 at 22:28

0 Answers0