i've got a problem with a custom TreeCellRenderer. the overridable method getTreeCellRendererComponent is executed about 4 time when i click on a node. Does anybody have an idea about this? Thanks by advance.
Simon
my code:
@Override
public Component getTreeCellRendererComponent(JTree tree, Object p_value,
boolean selected, boolean expanded, boolean leaf, int row,
boolean hasFocus) {
super.getTreeCellRendererComponent(tree, p_value, selected, expanded,
leaf, row, hasFocus);
if (row != -1) {
DefaultMutableTreeNode aNode = (DefaultMutableTreeNode) p_value;
TreePath treePath;
treePath = tree.getPathForRow(row);
if (treePath == null) {
return this;
}
// int pathCount = treePath.getPathCount();
JLabel label = (JLabel) this;
if (aNode.getUserObject() instanceof MlCompteMail) {
traiteNomCompte((MlCompteMail) aNode.getUserObject(), label);
// JTreeFactory treeFact = new JTreeFactory();
// treeFact.refreshNode(treePath);
return this;
} else if (aNode.getUserObject() instanceof MlDossier) {
traiteNomDossier((MlDossier) aNode.getUserObject(), label, leaf);
// JTreeFactory treeFact = new JTreeFactory();
// treeFact.refreshNode(treePath);
return this;
}
}
return this;
}
/**
* @param value
* @param treePath
* @param label
* @param p_leaf
*/
private void traiteNomDossier(MlDossier p_dossier, JLabel label,
boolean p_leaf) {
String nomDossier = p_dossier.getNomDossier();
if (nomDossier.equals(EnDossierBase.BROUILLON.getLib())) {
label.setIcon(IconeTreeFactory.getBrouillon());
} else if (nomDossier.equals(EnDossierBase.CORBEILLE.getLib())) {
label.setIcon(IconeTreeFactory.getCorbeille());
} else if (nomDossier.equals(EnDossierBase.ENVOYES.getLib())) {
label.setIcon(IconeTreeFactory.getEnvoye());
} else if (nomDossier.equals(EnDossierBase.RECEPTION.getLib())) {
label.setIcon(IconeTreeFactory.getReception());
} else if (nomDossier.equals(EnDossierBase.SPAM.getLib())) {
label.setIcon(IconeTreeFactory.getSpam());
} else if (p_leaf) {
label.setIcon(IconeTreeFactory.getDossierFerme());
} else {
label.setIcon(IconeTreeFactory.getDossierOuvert());
}
int unreadMess = p_dossier.getUnreadMessCount();
if (unreadMess > 0) {
label.setText(nomDossier + " (" + unreadMess + ")");
label.setFont(Fontfactory.getTREE_FONT_GRAS());
} else {
label.setText(nomDossier);
label.setFont(Fontfactory.getTREE_FONT_PLAIN());
}
return;
}
// }
// }
/**
* @param value
* @param label
*/
private void traiteNomCompte(MlCompteMail p_compteMail, JLabel label) {
int unreadMess = p_compteMail.getUreadMessCount();
if (unreadMess > 0) {
label
.setText(p_compteMail.getNomCompte() + " (" + unreadMess
+ ")");
label.setFont(Fontfactory.getTREE_FONT_GRAS());
} else {
label.setText(p_compteMail.getNomCompte());
label.setFont(Fontfactory.getTREE_FONT_PLAIN());
}
label.setIcon(IconeTreeFactory.getDossierOuvert());
}
i found a way to speed up the decoration of my jtree, the jtree use the method "toString()" to paint a node so i have override the method in my two kind of node, so, in my customTreeCellRenderer, i just call the toString method on my userObject and that work. Thanks for your help.