大家好我有一个带有Custom DefaultTreeTableModel的JXtreeTable。
现在我想要这个:
所以我有这段代码:
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) {}
}
使用此代码,我没有正确的布局。如果我尝试运行我的应用程序,我会看到这个
我无法理解为什么我看到这个图标也是对象的字符串,如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 .....我无法设置子节点的另一种字体。