2

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());

}
Simon Mardiné
  • 510
  • 2
  • 7
  • 23
  • setting an icon or not as such doesn't make a difference. Getting hold of the icon might. What looks really fishy in your code are the commented lines: what is the treeFactory and what does the refreshNode do with the treePath? – kleopatra Nov 08 '11 at 15:04

2 Answers2

3

That's probably not a problem; the method is called whenever the cell needs to be painted. You don't have the luxury of controlling that.

So design the method to be very efficient and make sure it doesn't have side effects, and you will have no problem.

Mark Peters
  • 80,126
  • 17
  • 159
  • 190
  • Thanks, but if i don't use my custom TreeCellRenderer, the collapsion/extension/selection of my node is quickly, so do you thinks it's because i use image Icon? is there a way to "say" that node have been already "decorated"? – Simon Mardiné Nov 08 '11 at 14:58
  • 1
    +1, just in nitpicking mood: not only when painted: it's called as well for querying the size of the node in order to build the LayoutCache – kleopatra Nov 08 '11 at 14:58
0

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.

Simon Mardiné
  • 510
  • 2
  • 7
  • 23