I have this code to create a custom JXTreeTable and set a Custom CellRenderer. This is the code:
tableCatSpese = new TableAlbero(null);
public TableAlbero(List<Conti> listaConti){
setTreeTableModel(new RandomTextTreeTableModel(listaConti));
this.setTreeCellRenderer(new CustomTreeTableSpeseXCategoriaSpese());
this.setDefaultRenderer(Object.class, new CustomTreeTableSpeseXCategoriaSpese());
}
}
class RandomTextTreeTableModel extends DefaultTreeTableModel {
public static NumberFormat decimalFormatter2 = new DecimalFormat("#,##0.00");
DefaultMutableTreeTableNode Root = null;
final String[] ColName = {"Acquisti/Vendite","Uscite","Entrate","Saldo"};
RandomTextTreeTableModel(List<Conti>listaConti) {
Root = new DefaultMutableTreeTableNode();
this.setRoot(Root);
Conti contoTotale = new Conti();
contoTotale.setTotale(0.0);
contoTotale.setTotalePubblico(0.0);
contoTotale.setEntrata(0.0);
contoTotale.setUscita(0.0);
contoTotale.setDescrizione("TOTALE");
if(listaConti!=null){
for (Conti conti : listaConti) {
DefaultMutableTreeTableNode node = new DefaultMutableTreeTableNode(conti);
Root.add(node);
if(conti.getListaContiFigli()!=null){
for (Conti contoFiglio : conti.getListaContiFigli()) {
node.add(new DefaultMutableTreeTableNode(contoFiglio));
}
}
contoTotale.setTotale(conti.getTotale()+contoTotale.getTotale());
contoTotale.setTotalePubblico(conti.getTotalePubblico()+contoTotale.getTotalePubblico());
contoTotale.setEntrata(conti.getEntrata()+contoTotale.getEntrata());
contoTotale.setUscita(conti.getUscita()+contoTotale.getUscita());
}
}
DefaultMutableTreeTableNode node = new DefaultMutableTreeTableNode(contoTotale);
Root.add(node);
}
@Override
public String getColumnName(int column){
return ColName[column];
}
@Override
public int getColumnCount() {
return ColName.length;
}
@Override
public Object getValueAt(Object arg0, int arg1) {
try{
if(arg1==0){
int numberElement = ((DefaultMutableTreeTableNode)arg0).getChildCount();
if(numberElement<=0)
return null;
Conti c = (Conti)((DefaultMutableTreeTableNode)arg0).getChildAt(0).getUserObject();
return new String(c.getDescrizione());
}
Conti c = (Conti)((DefaultMutableTreeTableNode)arg0).getUserObject();
if(c==null)
return null;
if (arg1 == 0) {
return c.getDescrizione();
}else if (arg1 == 1) {
return decimalFormatter2.format(c.getUscita());
}else if (arg1 == 2) {
return decimalFormatter2.format(c.getEntrata());
}else if (arg1 == 3) {
return decimalFormatter2.format(c.getTotale());
}
}catch(Exception e){
e.printStackTrace();
}
return arg0;
}
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);
@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 aValue,
boolean selected, boolean expanded, boolean leaf, int row, boolean hasFocus) {
JPanel panel = new JPanel(); // Create a new panel where we will show the data.
Conti conto = (Conti)((DefaultMutableTreeTableNode)aValue).getUserObject();
String text = conto.getDescrizione();
panel.add( new JLabel( text ) ); // Actually show the data.
// If the value is not null and is a tree node and a leaf then paint it.
if( (aValue != null) && (aValue instanceof DefaultMutableTreeTableNode) && leaf )
{
if(row != tree.getRowCount()){
//setForeground( Color.BLACK );
setBackground(new Color(200, 200, 200));
//setFont(UtilitySwing.getTableFont());
}else{
//setForeground(Color.BLACK);
setBackground( Color.RED );
//setFont(fontTotale);
}
panel.setEnabled( tree.isEnabled() );
panel.setBackground(Color.red);
return panel;
}
// For everything else use default renderer.
return defaultRenderer.getTreeCellRendererComponent( tree, aValue, selected, expanded, leaf,
row, hasFocus );
}
}
This is the graphic result:
How can you see int the first column, I can't see the complete String and I can't set correct background color.
How can I fixed this problem?