gpt4 book ai didi

java - Jtree getTreeCellRendererComponent 执行几次

转载 作者:行者123 更新时间:2023-11-29 06:07:39 25 4
gpt4 key购买 nike

我遇到了自定义 TreeCellRenderer 的问题。当我单击一个节点时,可覆盖方法 getTreeCellRendererComponent 执行大约 4 次。有人对此有想法吗?提前致谢。

西蒙

我的代码:

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

}

最佳答案

这可能不是问题;只要需要绘制单元格,就会调用该方法。您无法控制它。

所以将方法设计得非常高效,并确保它没有副作用,你就没有问题。

关于java - Jtree getTreeCellRendererComponent 执行几次,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8052169/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com