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:
The first row have the correct Font, and now I want to change the font of the child row.