我有一个JXTreeTable,并且实现了一个自定义的Renderer。

所以我有这段代码:

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);
        setHorizontalAlignment(SwingConstants.RIGHT);
        if (row== table.getRowCount()-1) {
            setForeground(Color.BLACK);
            setBackground( Color.RED );
            setFont(fontTotale);
        }else if(row != table.getRowCount() && column !=3){
            setForeground( Color.BLACK );
            setBackground(new Color(200, 200, 200));
            setFont(UtilitySwing.getTableFont());
        }else if(row != table.getRowCount()-1 && column ==3){
            //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;
    }

    public Component getTreeCellRendererComponent(JTree tree, Object value, boolean selected, boolean expanded, boolean leaf, int row, boolean hasFocus) {
        setOpaque(true);
        if (row== tree.getRowCount()-1) {
            setForeground(Color.BLACK);
            setOpaque(true);
            setBackground( Color.RED );
            setFont(fontTotale);
        }else if(row != tree.getRowCount()){
            setForeground( Color.BLACK );
            setBackground(new Color(200, 200, 200));
            setOpaque(true);
            setFont(UtilitySwing.getTableFont());
        }
         setText(value != null ? value.toString() : "<null>");
        return this;
    }
}


这是创建JXTreeTable的代码:

    public JTreeTable(){
JXTreeTable talbe = new JXTreeTable();
        talbe .setTreeTableModel(new RandomTextTreeTableModel(listaConti));
        talbe .setDefaultRenderer(Object.class, new CustomTreeTableSpeseXCategoriaSpese());
        talbe .setTreeCellRenderer(new CustomTreeTableSpeseXCategoriaSpese());
    }


如果我尝试运行此代码,则可以显示以下内容:
java - 如何设置JXTreeTable第一列的背景颜色-LMLPHP

如果我尝试打开第一个节点,则可以看到以下内容:
java - 如何设置JXTreeTable第一列的背景颜色-LMLPHP

我要这个:
1)显示全文
2)如何从图像中看到,我想在所有第一列中将背景色设置为灰色。我该如何解决?

编辑

该帖子不是重复的,因为在建议帖子中有JTree,但是在我的帖子中,我有JXTreeTable,这是不同的

最佳答案

这可以使用org.jdesktop.swingx.decorator.HighlightPredicate来完成

HighlightPredicate hlPred = new HighlightPredicate() {
    @Override
    public boolean isHighlighted(Component renderer,
            ComponentAdapter adapter) {
        return someCondition ? true : false;
    }
};

ColorHighlighter hl = new ColorHighlighter(hlPred, hlBgColor, hlFColor);
treeTable.addHighlighter(hl);


另见this SO question

08-04 06:00