如何将字体更改为特定行的JXteeTable

时间:2015-11-27 08:43:59

标签: java swing jxtreetable treecellrenderer

我的java swing应用程序中有一个JXtreeTable。 我想设置一个自定义的cellrenderer,所以我希望为父行设置一个自定义字体,我想为子节点(主节点)设置另一个自定义字体,但是我不能这样做。< / p>

这是我的代码:

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

如果我尝试运行此代码,我可以看到:

enter image description here

第一行有正确的Font,现在我想更改子行的字体。

0 个答案:

没有答案