如何在Java应用程序中设置getTreeCellRendererComponent

时间:2015-11-26 15:38:59

标签: java swing jxtreetable

大家好我有一个带有Custom DefaultTreeTableModel的JXtreeTable。

现在我想要这个:

  1. 使用打开的文件夹图标更改默认图标(当节点打开时),关闭文件夹图标(节点关闭时)
  2. 我希望在打开时更改子节点的所有Row渲染器。
  3. 所以我有这段代码:

    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) {}
    
        } 
    

    使用此代码,我没有正确的布局。如果我尝试运行我的应用程序,我会看到这个 enter image description here

    我无法理解为什么我看到这个图标也是对象的字符串,如com.mcsolution.beans.Entrata@11111

      

    编辑我用以下方法更改方法getTreeCellendereComponent:

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

    现在我可以看到正确的图标,但我继续看到写com.mcsolution .....我无法设置子节点的另一种字体。

0 个答案:

没有答案